Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^4: porting C code to Perl -- use integer bench

by Discipulus (Canon)
on Oct 24, 2017 at 07:12 UTC ( [id://1201948]=note: print w/replies, xml ) Need Help??


in reply to Re^3: porting C code to Perl -- solved
in thread porting C code to Perl

Hello again,

you guessed rigth! use integer; boosts performance (if I have build my benchmarks correctly..)

use strict; use warnings; use Benchmark qw(cmpthese timethese); ###################################################################### +########## my $results = timethese($ARGV[0]//-5, { 'no_integer_module ' =>\&no_integer, 'with_integer_module' =>\&with_integer, }); cmpthese( $results ); ###################################################################### +########## sub no_integer{ my $n=1002; my $len = 1 + 10 * $n / 3; my $pi; my @a= (2) x $len; my $nines = 0; my $predigit = 0; for (my $j =1; $j < $n + 1; ++$j){ my $q = 0; for my $i (reverse 0..$len - 1) { my $x = 10 * $a[$i] + $q * ($i + 1); my $divisor = 2 * $i + 1; $a[$i] = $x % $divisor; $q = $x / $divisor; } $a[0]=$q%10; $q=$q/10; if (9 == $q){ ++$nines; } elsif(10 == $q){ $pi.=$predigit + 1; for (my $k = 0; $k < $nines; $k++){$pi.=0} # print 0 $predigit = $nines = 0; } else{ $pi.= $predigit; $predigit = $q; if(0 != $nines){ for (my $k = 0; $k < $nines; $k++) { $pi.=9; } $nines = 0; } } } } ###################################################################### +########## sub with_integer{ use integer; my $n=1002; my $len = 1 + 10 * $n / 3; my $pi; my @a= (2) x $len; my $nines = 0; my $predigit = 0; for (my $j =1; $j < $n + 1; ++$j){ my $q = 0; for my $i (reverse 0..$len - 1) { my $x = 10 * $a[$i] + $q * ($i + 1); my $divisor = 2 * $i + 1; $a[$i] = $x % $divisor; $q = $x / $divisor; } $a[0]=$q%10; $q=$q/10; if (9 == $q){ ++$nines; } elsif(10 == $q){ $pi.=$predigit + 1; for (my $k = 0; $k < $nines; $k++){$pi.=0} # print 0 $predigit = $nines = 0; } else{ $pi.= $predigit; $predigit = $q; if(0 != $nines){ for (my $k = 0; $k < $nines; $k++) { $pi.=9; } $nines = 0; } } } }

Benchmark: running no_integer_module , with_integer_module for at lea +st 20 CPU seconds... no_integer_module : 21 wallclock secs (20.64 usr + 0.00 sys = 20.64 +CPU) @ 0.92/s (n=19) with_integer_module: 21 wallclock secs (20.50 usr + 0.00 sys = 20.50 +CPU) @ 1.37/s (n=28) s/iter no_integer_module with_integer_module no_integer_module 1.09 -- -33% with_integer_module 0.732 48% --

I read in the docs of the integer module:

> On many machines, this doesn't matter a great deal for most computations, but on those without floating point hardware, it can make a big difference in performance.

The above results show that my machine has not floating point hardware?

Thanks also for the link to the spigot explication; I must admit that yesterday night I've understood almost nothing.. I'll try under sun beans.

L*

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Replies are listed 'Best First'.
Re^5: porting C code to Perl -- use integer bench
by haukex (Archbishop) on Oct 24, 2017 at 18:22 UTC

    Your benchmark looks ok, thanks, although the difference is a little smaller on my machine (-21% / +27%). I did surround your three divisions with int() in the no_integer version.

    The above results show that my machine has not floating point hardware?

    I doubt it, rather I guess that Perl can simply use more efficient implementations of the mathematical opcodes, that don't need to worry about all the floating-point stuff that Perl hides in its numerical types.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1201948]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2024-04-23 19:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found