It sounds like you would want pack() or unpack() for a BigInt. But as far as I can see, the docs don't mention this as an option.

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:

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; }
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.

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:

@ulongs = unpack 'V*', $bytestring;
Of course, that means that you need to shift left by 32 bits, instead of by 8, per step.

In reply to Re: Converting an array of bytes into a BigInt - speed considerations by bart
in thread Converting an array of bytes into a BigInt - speed considerations by dempa

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.