in reply to Re: Should multiplication by integer be avoided in favour of division for performance reasons?
in thread Should multiplication by integer be avoided in favour of division for performance reasons?
Its your basic benchmark pitfall, the overhead overshadows the thing you're timing
#!/usr/bin/perl -- use strict; use warnings; use Benchmark 'cmpthese'; my @a = map log, 1..1e6; cmpthese( -2, { 1 => sub {[ map $_ * 4, @a ]}, 2 => sub {[ map $_ / (1/4), @a ]}, }); cmpthese( -2, { 1 => sub {[ map $_ * 4, @a ]}, 2 => sub {[ map $_ / (1/4), @a ]}, }); cmpthese( -2, { 1 => sub {[ map $_ * 4, @a ]}, 2 => sub {[ map $_ / (1/4), @a ]}, }); cmpthese( -2, { 1 => '[ map $_ * 4, @a ]', 2 => '[ map $_ / (1/4), @a ]', }); cmpthese( -2, { 1 => '[ map $_ * 4, @a ]', 2 => '[ map $_ / (1/4), @a ]', }); cmpthese( -2, { 1 => '[ map $_ * 4, @a ]', 2 => '[ map $_ / (1/4), @a ]', }); __END__
But as you can see, there is no difference between the two
Rate 1 2
1 1.73/s -- -18%
2 2.11/s 22% --
Rate 1 2
1 1.70/s -- -19%
2 2.11/s 24% --
Rate 1 2
1 1.75/s -- -17%
2 2.10/s 20% --
Rate 1 2
1 2371151/s -- -3%
2 2438111/s 3% --
Rate 2 1
2 2272556/s -- -8%
1 2473993/s 9% --
Rate 2 1
2 2140327/s -- -8%
1 2313868/s 8% --
|
|---|