Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Re: Difference between tr// and s///?

by HyperZonk (Friar)
on Feb 06, 2004 at 16:19 UTC ( [id://327116]=note: print w/replies, xml ) Need Help??


in reply to Re: Difference between tr// and s///?
in thread Difference between tr/// and s///?

Hmmm ... it seems to me that your benchmarks are actually indicating that tr/// is slower than s///. Not what I would have expected, and apparently no one else did either, since despite the evidence, everyone is claiming that this test shows that tr/// is faster.

Update: I just ran the benchmark on my ActiveState build and got similar results ... that, strangely, s appears to be nearly twice as fast as tr in this trivial example. Results showing only about 1 million tr's per second vs. almost 2 million s's per second:

Rate transliterate substitution transliterate 1011345/s -- -49% substitution 1969797/s 95% --

Just for background, this is ActivePerl 5.8.0 build 806.

UPDATE Update: Thanks to davido for pointing us in the right direction on this in the CB before he posted his node.

use Benchmark qw(:all); $toto = 'this+is+my+text+and+here+is+more+and+more+this+is+my+text+and ++here+is+more+and+more+this+is+my+text+and+here+is+more+and+more'; $count =-5; $results = timethese($count, { 'transliterate' => sub { $toto = 'this+is+my+text+and+ +here+is+more+and+more+this+is+my+text+and+here+is+more+and+more+this+ +is+my+text+and+here+is+more+and+more'; $toto =~tr/+/ /; }, 'substitution' => sub { $toto = 'this+is+my+text+and+h +ere+is+more+and+more+this+is+my+text+and+here+is+more+and+more+this+i +s+my+text+and+here+is+more+and+more'; $toto =~s/\+/ /g; }, }, 'none' ); cmpthese( $results ) ; exit 0;
Now we have the same string for the benchmark tests! And the results are as expected:
Rate substitution transliterate substitution 73807/s -- -88% transliterate 625256/s 747% --

Boy, do I feel stupid for not seeing that!


-HZ

Replies are listed 'Best First'.
Re: Re: Re: Difference between tr// and s///?
by Sol-Invictus (Scribe) on Feb 06, 2004 at 16:29 UTC
    Rate matching Indexing matching 695587/s -- -41% Indexing 1187633/s 71% --
    it reads right to left, rather than horzontally and vertically:
    matching is 41% slower than indexing
    Indexing is 71% faster than matching

    Sol-Invictus

    You spend twenty years learning the spell that makes nude virgins appear in your bedroom, and then you're so poisoned by quicksilver fumes and half-blind from reading old grimoires that you can't remember what happens next.

      Sol-Invictus:

      I was speaking to the tr vs. s, not the s vs. index.


      -HZ

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-19 20:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found