Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

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

by davido (Cardinal)
on Feb 06, 2004 at 16:43 UTC ( [id://327134]=note: print w/replies, xml ) Need Help??


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

Your benchmark is flawed. On the first iteration of the testing, your $toto variable has all of its '+' characters changed to ' ' (space) characters. After that, all subsequent iterations and tests are acting upon the "fixed" string, and thus, they basically have no more work to do other than their own internal overhead.

For your benchmark to gain validity, you'll need to make a copy of $toto, to act upon, inside each test, so that the original $toto is always in its original condition. Or declare and define $toto inside of each individual sub being tested.


Dave

  • Comment on Re: Re: Difference between tr// and s///?

Replies are listed 'Best First'.
Re: Re: Re: Difference between tr// and s///?
by Limbic~Region (Chancellor) on Feb 06, 2004 at 16:45 UTC
    davido,
    You are correct as the following modified code shows:
    #!/usr/bin/perl use strict; use warnings; use Benchmark qw(:all); my $length = (int rand 100) + 100; my @char = ( 'a' .. 'c' , '+' , 'd' .. 'f' ); my $string; $string .= $char[ int rand 7 ] for 0 .. $length ; my $count = -5; my $results = timethese ( $count, { 'transliterate' => sub { my $foo = $string; $foo =~ tr/ ++/ /; }, 'substitution' => sub { my $bar = $string; $bar =~s/\+ +/ /g; }, }, 'none' ); cmpthese( $results ) ; __END__ Rate substitution transliterate substitution 124624/s -- -84% transliterate 784349/s 529% --
    Cheers - L~R

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-04-25 23:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found