in reply to Converting an array of bytes into a BigInt - speed considerations
OK, perhaps a slightly faster implementation might be to replace the multiplication by a shift left. And definitely drop the exponentiation if you can. I suspect this is the real cause for the long processing time.
Er... you have a Little Endian representation in the "byte array", haven't you? Big Endian is easier to process. OK, here I go:
To be honest, I'd rather have replaced the overloaded operators with explicit calls to blsft and badd, but for some reason unknown to me these appear to return simple strings, not Math::BigInt objects, as I gather from the docs they should. (BTW I'm using 0.01, as it comes with perl 5.6.1.) Oh well. I hope this is a decent starting point anyway.use Math::BigInt; print bytearray_to_bigint(127,2); use Data::Dumper; sub bytearray_to_bigint { my @array = reverse @_; #Big Endian my $result = Math::BigInt->new('0'); foreach my $a (@array) { ($result <<= 8) += $a; # This alternative doesn't work: # $result = $result->blsft(8)->badd($a); } return $result; }
Update: I have the impression that Math::BigInt is a Pure Perl implementation by default. Ugh! If you use it with one of the XS "driver" libraries, you might get a real speed boost. See the section on "MATH LIBRARY" in the new docs. Math::BigInt::BitVect+Bit::Vector and Math::BigInt::FastCalc look like the most promising candidates to me. You might have to download an update to Math::BigInt to make use of it. They are available for Windows, Perl 5.6.x at the 5.6plus repository. Ooh, "Math::BigIntFast" looks nice, too, even though I can't find a trace of it on CPAN. Odd.
Update: It suddenly dawned on me that it's easy to get up to a factor 4 of speedup, by unpacking your byte stream into long integers (unsigned 32 bits) instead of in bytes. That way, you can construct your bigint in 4 bytes per step, instead of just 1 byte for each loop.
Unpacking a string to longs, on a PC (Little Endioan), can be done using:
Of course, that means that you need to shift left by 32 bits, instead of by 8, per step.@ulongs = unpack 'V*', $bytestring;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Converting an array of bytes into a BigInt - speed considerations
by dempa (Friar) on Jan 23, 2003 at 16:02 UTC | |
by bart (Canon) on Jan 23, 2003 at 16:37 UTC | |
by dempa (Friar) on Jan 23, 2003 at 17:35 UTC |