Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
And especially thanks to salva

I'll second that !!

There are of course, more things to consider than just *performance* when it comes to choosing which Math module to use - but I was curious to see how Math::Int128 compared for speed with Math::GMPz (which uses the gmp C library).

I found that, if using overloaded operators with Math::GMPz, then Math::GMPz was roughly twice as slow as Math::Int128 - but if using the arithmetic functions that Math::GMPz also provides, then Math::GMPz was roughly twice as fast as Math::Int128.

Without yet having taken the time to look closely at Math::Int128, I believe there would be a way of having it out-perform Math::GMPz. (And I'm about to embark upon a little excercise to satisfy myself that this is so.)
For any Windows users who want to check the comparisons for themselves, there are ppm packages for 64-bit perl (5.10 and 5.12) available:
ppm install ppm install
The benchmarks I ran are as follows:
use warnings; use Math::Int128 qw(int128); use Math::GMPz qw(:mpz); use Benchmark qw(:all); $count = 40000; $mpz1 = Math::GMPz->new('676469752303423489'); $mpz2 = Math::GMPz->new('776469752999423489'); $i_1 = int128("$mpz1"); $i_2 = int128("$mpz2"); $mpz_sub = Math::GMPz->new('976469752313423489'); $i_sub = int128("$mpz_sub"); $mpz_div = Math::GMPz->new('76469752313423489'); $i_div = int128("$mpz_div"); $mpz_ret = Rmpz_init2(128); print " ****************** **MULTIPLICATION** ******************\n\n"; cmpthese($count * 9, { 'mul_M::I' => '$ri = Math::Int128::_mul($i_1, $i_2, 0)', 'mul_M::G1' =>'$mpz_ret = $mpz1 * $mpz2', 'mul_M::G2'=> 'Rmpz_mul($mpz_ret, $mpz1, $mpz2)', }); die "Error 1:\n$ri\n$mpz_ret\n" if $ri != int128("$mpz_ret") || $ri != int128('525258301482620425304858018020933121'); $i_1 *= $i_1; $i_2 *= $i_2; $mpz1 *= $mpz1; $mpz2 *= $mpz2; print " ****************** *****DIVISION***** ******************\n\n"; cmpthese($count * 9, { 'div_M::I' => '$ri = Math::Int128::_div($i_1, $i_div, 0)', 'div_M::G1' =>'$mpz_ret = $mpz1 / $mpz_div', 'div_M::G2'=> 'Rmpz_tdiv_q($mpz_ret, $mpz1, $mpz_div)', }); die "Error 2:\n$ri\n$mpz_ret\n" if $ri != int128("$mpz_ret") || $ri != int128('5984213521522366751'); print" ****************** *****ADDITION***** ******************\n\n"; cmpthese($count * 10, { 'add_M::I' => '$ri = Math::Int128::_add($i_1, $i_2, 0)', 'add_M::G1' => '$mpz_ret = $mpz1 + $mpz2', 'add_M::G2' => 'Rmpz_add($mpz_ret, $mpz1, $mpz2)', }); die "Error 3:\n$ri\n$mpz_ret\n" if $ri != int128("$mpz_ret") || $ri != int128('1060516603104440851094132036041866242'); print " ****************** ****SUBTRACTION*** ******************\n\n"; cmpthese($count * 10, { 'add_M::I' => '$ri = Math::Int128::_sub($i_1, $i_sub, 0)', 'add_M::G1' => '$mpz_ret = $mpz1 - $mpz_sub', 'add_M::G2' => 'Rmpz_sub($mpz_ret, $mpz1, $mpz_sub)', }); die "Error 4:\n$ri\n$mpz_ret\n" if $ri != int128("$mpz_ret") || $ri != int128('457611325781455127825205517363509632');
The output I got was:
C:\_64\pscrpt>perl Name "main::i_div" used only once: possible typo at line 17. Name "main::i_sub" used only once: possible typo at line 14. ****************** **MULTIPLICATION** ****************** Rate mul_M::G1 mul_M::I mul_M::G2 mul_M::G1 156726/s -- -58% -82% mul_M::I 371517/s 137% -- -58% mul_M::G2 886700/s 466% 139% -- ****************** *****DIVISION***** ****************** Rate div_M::G1 div_M::I div_M::G2 div_M::G1 148637/s -- -57% -81% div_M::I 344168/s 132% -- -57% div_M::G2 794702/s 435% 131% -- ****************** *****ADDITION***** ****************** Rate add_M::G1 add_M::I add_M::G2 add_M::G1 148810/s -- -60% -83% add_M::I 376648/s 153% -- -56% add_M::G2 852878/s 473% 126% -- ****************** ****SUBTRACTION*** ****************** Rate add_M::G1 add_M::I add_M::G2 add_M::G1 147113/s -- -60% -83% add_M::I 365631/s 149% -- -59% add_M::G2 883002/s 500% 142% -- C:\_64\pscrpt>
Update: I've since written an Inline::C script where the __int128 multiplications beat Math::GMPz ... but not by much:
Rate gmpz mul_128 gmpz 900000/s -- -3% mul_128 927835/s 3% --
The code I used is still a bit rough, but I don't see much scope for improvement at the moment.


In reply to Re^12: Module for 128-bit integer math? by syphilis
in thread Module for 128-bit integer math? by BrowserUk

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others meditating upon the Monastery: (3)
    As of 2020-11-28 06:15 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found