Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^5: Reversed .= operator

by choroba (Cardinal)
on Apr 29, 2016 at 22:44 UTC ( [id://1161930]=note: print w/replies, xml ) Need Help??


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

Here's the benchmark code:
#!/usr/bin/perl use warnings; use strict; use Benchmark qw{ cmpthese }; 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 } use Test::More; is precat('def', 'abc'), 'abcdef', 'correct concat'; is Substr('def', 'abc'), 'abcdef', 'same substr'; is subst('def', 'abc'), 'abcdef', 'same subst'; done_testing(3); cmpthese( -3, { precat => q( precat 'def', 'abc' ), substr => q( Substr 'def', 'abc' ), subst => q( subst 'def', 'abc' ), });

Here's the result on my machine:

ok 1 - correct concat ok 2 - same substr ok 3 - same subst 1..3 Rate subst precat substr subst 1770916/s -- -38% -47% precat 2860656/s 62% -- -15% substr 3350927/s 89% 17% --

($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,

Replies are listed 'Best First'.
Re^6: Reversed .= operator
by BrowserUk (Patriarch) on Apr 30, 2016 at 03:24 UTC

    The results are even more dramatic if you do away with some of the dogma.

    The overhead of the subroutine calls may be a constant; but it is sufficiently large that it obscures the real differences. Particularly the dramatic cost of using subst for such trivial operations:

    #!/usr/bin/perl use warnings; use strict; our $TEST //= 0; use Benchmark qw{ cmpthese }; 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 } sub pc(\$@){ ${$_[0]} = $_[1] . ${$_[0]}; } sub sr(\$@){ substr ${$_[0]}, 0, 0, $_[1]; } sub st(\$@){ ${$_[0]} =~ s[^][$_[1]]; } cmpthese -1, { precat1 => q( precat 'def', 'abc' ), substr1 => q( Substr 'def', 'abc' ), subst1 => q( subst 'def', 'abc' ), precat2 => q( my $r = 'def'; pc $r, 'abc'; ), substr2 => q( my $r = 'def'; sr $r, 'abc'; ), subst2 => q( my $r = 'def'; st $r, 'abc'; ), precat3 => q( my $r = 'def'; $r = 'abc' . $r; ), substr3 => q( my $r = 'def'; substr $r, 0, 0, 'abc'; ), subst3 => q( my $r = 'def'; $r =~ s[^][abc]; ), }; __END__ Rate subst2 subst1 subst3 precat1 precat2 substr1 precat3 + substr2 substr3 subst2 338979/s -- -40% -46% -61% -73% -74% -77% + -78% -91% subst1 563106/s 66% -- -10% -35% -55% -57% -61% + -63% -85% subst3 625570/s 85% 11% -- -28% -50% -52% -57% + -59% -84% precat1 863641/s 155% 53% 38% -- -31% -34% -40% + -44% -77% precat2 1242736/s 267% 121% 99% 44% -- -6% -14% + -19% -67% substr1 1315147/s 288% 134% 110% 52% 6% -- -9% + -14% -66% precat3 1442465/s 326% 156% 131% 67% 16% 10% -- + -6% -62% substr2 1537398/s 354% 173% 146% 78% 24% 17% 7% + -- -60% substr3 3816524/s 1026% 578% 510% 342% 207% 190% 165% + 148% --

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re^6: Reversed .= operator
by marioroy (Prior) on Apr 30, 2016 at 19:38 UTC

    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% --


      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://1161930]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-23 16:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found