in reply to Re^3: Should multiplication by integer be avoided in favour of division for performance reasons? (benchmark pitfalls)
in thread Should multiplication by integer be avoided in favour of division for performance reasons?
use Benchmark 'cmpthese'; my $x = 1; $x = 1.1; # $x promoted to PVNV our @a = map $x, 1..1e6; my $y = 1.1; our @b = map $y, 1..1e6; cmpthese( -2, { 1 => '{my @c = @a}', 2 => '{my @c = @b}', __END__ Rate 1 2 1 26.7/s -- -35% 2 40.9/s 53% --
As it happens, perl's multiply operator is optimised to handle int*int and float*float quickly; other permutations like int*float and float*string take slower paths. As it also happens, if you do $float * 4, the constant 4 is internally upgraded to hold both an IV and NV value, so subsequent iterations take the fast float*float code path.
Dave.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: Should multiplication by integer be avoided in favour of division for performance reasons? (benchmark pitfalls)
by vr (Curate) on Nov 29, 2019 at 16:24 UTC | |
by dave_the_m (Monsignor) on Nov 29, 2019 at 19:32 UTC |