Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^6: Reversed .= operator

by marioroy (Prior)
on Apr 30, 2016 at 19:38 UTC ( [id://1161967]=note: print w/replies, xml ) Need Help??


in reply to Re^5: Reversed .= operator
in thread Reversed .= operator

Below, benchmarked the same 3 functions with minimal variable declaration.

use warnings; use strict; use Benchmark qw{ cmpthese }; # original code by choroba sub precat { my ($string, $prefix) = @_; $string = $prefix . $string; $string } sub Substr { my ($string, $prefix) = @_; substr $string, 0, 0, $prefix; $string } sub subst { my ($string, $prefix) = @_; $string =~ s/^/$prefix/; $string } # minimal variable declaration sub precatm { $_[1] . $_[0]; } sub Substrm { substr my $string = $_[0], 0, 0, $_[1]; $string; } sub substm { ( my $string = $_[0] ) =~ s/^/$_[1]/; $string; } cmpthese -1, { precat => q( precat 'def', 'abc' ), substr => q( Substr 'def', 'abc' ), subst => q( subst 'def', 'abc' ), precatm => q( precatm 'def', 'abc' ), substrm => q( Substrm 'def', 'abc' ), substm => q( substm 'def', 'abc' ), };

Results from a 2.6 GHz Core i7 CPU:

Rate subst substm precat substr substrm precatm subst 2104367/s -- -10% -29% -48% -61% -72% substm 2338582/s 11% -- -21% -43% -57% -69% precat 2969268/s 41% 27% -- -27% -45% -61% substr 4071762/s 93% 74% 37% -- -25% -46% substrm 5397081/s 156% 131% 82% 33% -- -29% precatm 7561845/s 259% 223% 155% 86% 40% --


Replies are listed 'Best First'.
Re^7: Reversed .= operator
by choroba (Cardinal) on Apr 30, 2016 at 19:45 UTC
    But your precatm only measures the . operator, not the =. operator :-)
    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

      Understood. I was comparing implementations for prepending a string to a string. If made into a function, why not do this :)

      $_[1] . $_[0]

      Basically, wanted to see if faster and by how much.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1161967]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-04-23 06:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found