http://www.perlmonks.org?node_id=882302


in reply to Re^2: Near equal partitions.
in thread Near equal partitions.

It is probably because I'm using a 64-bit build? For reference, here is my benchmark code:

#! perl -slw use strict; use Benchmark qw[ cmpthese ]; use POSIX qw(ceil); sub buk{ my( $n, $m ) = @_; my @parts = (int( $n / $m )) x $m; $n -= $_ for @parts; my $i =0; $parts[ $i++ ]++ while $n--; return @parts; } sub eli { my ($n, $m) = @_; my $r = $n%$m; # <== my @parts = (($n-$r)/$m) x $m; # <== my $i=0; $parts[$i++]++ while $r--; return @parts; } sub syp { my ($n, $m) = @_; my @parts; for(0 .. $m - 1) { $n -= $parts[$_] = ceil($n / $m); $m -= 1; } return @parts; } sub ike { my ($n, $m) = @_; my $q = int($n / $m); my $r = $n % $m; return ($q+1) x $r, ($q) x ($m-$r); } printf "buk: 97/$_ [%s]\n", join ' ', buk( 97, $_ ) for 2 .. 9; printf "eli: 97/$_ [%s]\n", join ' ', eli( 97, $_ ) for 2 .. 9; printf "syp: 97/$_ [%s]\n", join ' ', syp( 97, $_ ) for 2 .. 9; printf "ike: 97/$_ [%s]\n", join ' ', ike( 97, $_ ) for 2 .. 9; cmpthese -1, { buk => q[ for my $n ( 2 .. 100 ) { for my $m ( 3 .. 50 ) { my @n = buk( $n, $m ); } } ], eli => q[ for my $n ( 2 .. 100 ) { for my $m ( 3 .. 50 ) { my @n = eli( $n, $m ); } } ], syp => q[ for my $n ( 2 .. 100 ) { for my $m ( 3 .. 50 ) { my @n = syp( $n, $m ); } } ], ike => q[ for my $n ( 2 .. 100 ) { for my $m ( 3 .. 50 ) { my @n = ike( $n, $m ); } } ], }

With regard to the integer/float division performance. Regardless of any raw machine-level differences in integer/double division--which these days are far less pronounced than previously--the cost of Perl math is always dominated by the number of Perl opcodes to perform the calculation, not the raw machine code performance.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.