Perl binary vs. shift multiplication Benchmark

A little Benchmark about the binary and the multiplication operator in Perl:

#!/usr/bin/perl
use strict;
use Benchmark qw(:all) ;
my $a = 100;
my $x = 0;
print "".($a *  4)."\n";
print "".($a << 2)."\n";
 
cmpthese(-2, {
'mul'  => sub {$x = $a *  4;},
'bin'  => sub {$x = $a << 2;},
});

Our result:

          Rate  bin  mul 
bin 12728559/s   -- -52% 
mul 26530177/s 108%   --

We see its better to avoid the binary options.

3 thoughts on “Perl binary vs. shift multiplication Benchmark”

  1. Right here is the perfect website for anyone who wishes to find out about this topic.
    You realize so much its almost hard to argue
    with you (not that I actually will need toHaHa).
    You certainly put a brand new spin on a subject that’s been written about for many years.

    Wonderful stuff, just excellent!

Leave a Reply

Your email address will not be published.

five × three =