in reply to Optimizations and Efficiency
$x = $y * 16; $x = $y << 4; #Much faster
This is an example (like most) where you can't tell what Perl's going to do. It turns out to only be 12% faster for the example you gave (which I don't consider an order of magnitude). Without "use integer", shift is only about 16% faster. Which I wouldn't have expected, either.
Benchmark: running multiply, shift, each for at least 10 CPU seconds... multiply: 12 wallclock secs (10.87 usr + 0.00 sys = 10.87 CPU) @ 798444.43/s (n=8679091) shift: 10 wallclock secs (10.57 usr + -0.01 sys = 10.56 CPU) @ 894401.80/s (n=9444883) Rate multiply shift multiply 798444/s -- -11% shift 894402/s 12% --
use Benchmark; use integer; Benchmark::cmpthese(-10, { multiply => sub { my $y = 3; my $x = $y * 16 }, shift => sub { my $y = 3; my $x = $y << 4 } } );
|
---|