Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: fast bit twiddling

by emazep (Priest)
on May 03, 2004 at 19:42 UTC ( #350120=note: print w/ replies, xml ) Need Help??


in reply to fast bit twiddling

Inspired by BrowserUK's 2nd algorithm but slightly faster, since it calls substr just once in exchange for an extra assignment:

sub emazep { my $s = shift; my $tmp; ( ($tmp = substr $s, $_-1, 2) eq '11' or $tmp eq '00' ) and return +for @_; return 1; }
Using exactly the same benchmark provided here by BrowserUK, it gives:
Rate ysth japhy matija buk2 buk1 emazep ysth 1966/s -- -17% -87% -89% -89% -91% japhy 2361/s 20% -- -84% -87% -87% -89% matija 14719/s 648% 523% -- -19% -20% -32% buk2 18088/s 820% 666% 23% -- -1% -17% buk1 18363/s 834% 678% 25% 2% -- -16% emazep 21753/s 1006% 821% 48% 20% 18% --

Update:

added esialb's (corrected) solution to the BrowserUK's benchamrk:
Rate ysth japhy matija esialb buk1 buk2 emazep ysth 1975/s -- -17% -87% -88% -89% -89% -91% japhy 2374/s 20% -- -84% -85% -87% -87% -89% matija 15000/s 659% 532% -- -8% -17% -18% -30% esialb 16299/s 725% 586% 9% -- -10% -11% -23% buk1 18156/s 819% 665% 21% 11% -- -1% -15% buk2 18277/s 825% 670% 22% 12% 1% -- -14% emazep 21295/s 978% 797% 42% 31% 17% 17% --

2nd Update:

I've been misleaded by the BrowserUK solution, which contains an error which also slowed down his sub (see here.) After the correction his sub proves to be the fastest (dani_l's very elegant solution also included in the benchmark):
Rate ysth japhy esialb matija buk1 dani_l emazep buk2* ysth 1845/s -- -22% -85% -87% -88% -89% -90% -91% japhy 2366/s 28% -- -81% -84% -84% -86% -87% -88% esialb 12530/s 579% 430% -- -14% -16% -28% -31% -38% matija 14566/s 689% 516% 16% -- -2% -16% -20% -28% buk1 14885/s 707% 529% 19% 2% -- -14% -18% -27% dani_l 17357/s 841% 634% 39% 19% 17% -- -5% -14% emazep 18181/s 885% 669% 45% 25% 22% 5% -- -10% buk2* 20275/s 999% 757% 62% 39% 36% 17% 12% --
buk2* is the buk2 sub as corrected by me here.

Cheers, Emanuele.


Comment on Re: fast bit twiddling
Select or Download Code
Re: Re: fast bit twiddling
by dani_l (Initiate) on May 04, 2004 at 05:35 UTC
    Refinement of emazep's:
    sub dani_l { my $s = shift; substr($s, $_ - 1, 2) =~ tr/1// == 1 or return 0 for @_; return 1; }

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (7)
As of 2015-07-06 07:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (70 votes), past polls