Perl string concat vs. multiplication benchmark

Someone wanted me to test if it work to speed up the multiplication by 10 to add the string ‘0’ to the end:

use strict;
use Benchmark qw(:all) ;
my $a = 100;
my $x = 100;
print "".($a *  10)."\n";
print "".($a . '0')."\n";

cmpthese(-2, {
'mul'  => sub {$x = $a *  10;},
'con'  => sub {$x = $a . '0';},

Our result:

          Rate  con  mul 
con 10268075/s   -- -58% 
mul 24702018/s 141%   --

It was not a very good idea because its very slow …. better use the normal way.

2 thoughts on “Perl string concat vs. multiplication benchmark”

  1. After checking out a handful of the blog articles
    on your website, I seriously appreciate your way of writing a blog.

    I saved as a favorite it to my bookmark site list and will be checking
    back in the near future. Please visit my website too and let me know your

  2. Thanks a bunch for sharing this with all of us you really know what you’re
    speaking about! Bookmarked. Kindly additionally discuss with my site
    =). We could have a hyperlink alternate agreement between us

Leave a Reply

Your email address will not be published.

5 × 3 =