Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^2: In-place bitwise NOT?

by choroba (Cardinal)
on Jul 26, 2013 at 18:17 UTC ( [id://1046579]=note: print w/replies, xml ) Need Help??


in reply to Re: In-place bitwise NOT?
in thread In-place bitwise NOT?

Nice! Benchmark of various solutions (I hope I did not spoil it again):
#!/usr/bin/perl use warnings; use strict; use Test::More tests => 5; use Benchmark qw(cmpthese); my $bits = join q(), map chr rand 256, 1 .. 1e7; sub single { $_[0] =~ s/(.)/~$1/ges; } sub long { $_[0] =~ s/(.{1,10000})/~$1/ges; } sub classic { $_[0] = ~$_[0]; } sub str { my $blocksize = 10000; my $lb = length $_[0]; my $offset = 0; while( $offset < $lb ) { substr $_[0], $offset, $blocksize, ~substr( $_[0], $offset, $blocksize ); $offset += $blocksize; } } BEGIN { my $chars = join q(), map sprintf('\x%02x', $_), 0 .. 255; my $rev = join q(), map sprintf('\x%02x', $_), reverse 0 .. 255; eval "sub translate { \$_[0] =~ tr/$chars/$rev/ } ; 1 " or die $@; } my $copy = $bits; classic($copy) for 1 .. 2; is($bits, $copy, 'double negation'); single($copy); classic($bits); is($bits, $copy, 'single - classic'); classic($bits); long($copy); is($bits, $copy, 'classic - long'); classic($bits); translate($copy); is($bits, $copy, 'classic - translate'); classic($bits); str($copy); is($bits, $copy, 'classic - str'); cmpthese(-10, { classic => sub { classic($bits) }, single => sub { single($bits) }, long => sub { long($bits) }, translate => sub { translate($bits) }, str => sub { str($bits) }, } );

Output on my machine:

1..5 ok 1 - double negation ok 2 - single - classic ok 3 - classic - long ok 4 - classic - translate ok 5 - classic - str Rate single translate long classic str single 0.328/s -- -100% -100% -100% -100% translate 161/s 48834% -- -26% -39% -59% long 217/s 66040% 35% -- -18% -45% classic 263/s 80093% 64% 21% -- -33% str 394/s 119839% 145% 81% 50% --
لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Replies are listed 'Best First'.
Re^3: In-place bitwise NOT?
by BrowserUk (Patriarch) on Jul 26, 2013 at 19:24 UTC

    Here's my version of that. The first run is on a 1MB string will all tests enabled.

    The second run is on a 1GB string with the classic and s/// solutions disable because life's too short :)

    C:\test>1046579 -N=2**20 1048576 Rate single long classic str translate single 1.72/s -- -99% -100% -100% -100% long 134/s 7707% -- -71% -79% -81% classic 468/s 27047% 248% -- -26% -33% str 632/s 36579% 370% 35% -- -9% translate 695/s 40233% 417% 49% 10% -- C:\test>1046579 -N=2**30 1073741824 (warning: too few iterations for a reliable count) (warning: too few iterations for a reliable count) s/iter str translate str 1.96 -- -21% translate 1.54 27% --

    The modified code:


    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".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      I am confused. Is not $_[0] aliased to the function argument? Why using all those references and dereferences?
      Moreover, running your code with N=2**29, I am still getting
      single 6.13e-03/s -- -100% -100% -100% -100% long 2.97/s 48381% -- -1% -35% -58% translate 3.01/s 48989% 1% -- -35% -58% classic 4.60/s 74981% 55% 53% -- -36% str 7.14/s 116486% 140% 137% 55% --

      With 2**30, it dies with

      Substitution loop at ./7.pl line 10.

      Update: This is perl 5, version 16, subversion 0 (v5.16.0) built for x86_64-linux-thread-multi

      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
        Is not $_[0] aliased to the function argument? Why using all those references and dereferences?

        Yes, it is aliased. But many people, including myself to some degree, have an aversion to using $_[n] in subroutines and will routinely change them to named vars if the subroutines become more complex than simple one liners.

        Hence, when it is important to avoid copying scalar arguments, I prefer to use explicit references rather than implicit aliasing.

        Moreover, running your code with N=2**29, I am still getting

        I have no explanation for that. This is 5.18, with single and long excluded because they just take too long to run on strings of this length:

        C:\test>\perl5.18\bin\perl.exe 1046579.pl -N=2**20 Rate classic translate str classic 482/s -- -30% -34% translate 688/s 43% -- -6% str 730/s 52% 6% -- C:\test>\perl5.18\bin\perl.exe 1046579.pl -N=2**29 Rate classic str translate classic 0.945/s -- -21% -26% str 1.20/s 27% -- -6% translate 1.27/s 35% 6% -- C:\test>\perl5.18\bin\perl.exe 1046579.pl -N=2**30 s/iter classic str translate classic 2.06 -- -19% -26% str 1.68 23% -- -9% translate 1.53 35% 10% -- C:\test>\perl5.18\bin\perl.exe 1046579.pl -N=2**31 s/iter str classic translate str 3.97 -- -7% -23% classic 3.69 8% -- -17% translate 3.06 30% 20% --

        On my machine tr/// wins hands down for these longer lengths. Which is what I would expect everywhere.

        It really surprises me that sub str does as well as it does for as long as it does given this complexity of opcodes from the explicit loop:

        Compared the implicit loop of translate:

        C:\test>perl -MO=Concise,translate 1046579.pl main::translate: 8 <1> leavesub[1 ref] K/REFC,1 ->(end) - <@> lineseq KP ->8 1 <;> nextstate(main 1267 (eval 7):1) v:*,&,{,$ ->2 - <1> null KS/2 ->- 6 <1> rv2sv sKRM/3 ->7 - <@> scope sK ->6 - <0> ex-nextstate v ->2 5 <2> aelem sKM/DREFSV,2 ->6 3 <1> rv2av sKR/3 ->4 2 <#> gv[*_] s ->3 4 <$> const[IV 0] s ->5 7 <"> trans sS/COMPL ->8 1046579.pl syntax OK

        And str() is still somewhat light on logic as it currently makes no attempt to deal with misalignments between the string length and the buffer size. I haven't checked to see what effect that has on the length of the string.

        Why you would get different results to me for these large size strings; I cannot begin to guess?

        With 2**30, it dies with Substitution loop at ./7.pl line 10.

        I never saw that because I disabled that test along with single (for taking too long) before the error ever manifest itself.

        I can confirm that I also get the error message once the length of the string moves above 2**29:

        C:\test>\perl5.18\bin\perl.exe 1046579.pl -N=2**29 s/iter long classic str translate long 3.92 -- -74% -78% -81% classic 1.03 282% -- -17% -29% str 0.847 363% 21% -- -14% translate 0.731 436% 40% 16% -- C:\test>\perl5.18\bin\perl.exe 1046579.pl -N=2**30 Substitution loop at 1046579.pl line 42.

        I have no idea what it means?


        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".
        In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2024-04-23 12:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found