Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Re: Re: Re: Re: string manipulation

by Xxaxx (Monk)
on Mar 30, 2001 at 03:56 UTC ( #68242=note: print w/ replies, xml ) Need Help??


in reply to Re: Re: Re: Re: string manipulation
in thread string manipulation

Hey Extremely, Thanks for taking a look at the code and letting me know where the benchmark may have messed up:

#!/usr/local/bin/perl -w use strict; use Benchmark; my $count =500000; ## Method number one sub One { my $data = 'for bar baz'; my($outstring); ($outstring = $data) =~ tr/-/_/; } ## Method number two sub Two { my $data = 'for bar baz'; my($outstring) = $data; $outstring =~ s/-/_/g; } ## We'll test each one, with simple labels timethese ( $count, {'Method One TR' => '&One', 'Method Two S' => '&Two', } ); exit;

Claude


Comment on Re: Re: Re: Re: Re: string manipulation
Download Code
Re: Re: Re: Re: Re: Re: string manipulation
by extremely (Priest) on Mar 30, 2001 at 04:23 UTC
    Your test strings don't have any '-' hyphens in them. =) And you change assignment forms between the two subs which honestly shouldn't have much effect but is still questionable practice when benchmarking since you want the code identical in nature except for the key point you are testing...

    Also, since the $data is set in the sub it will be reset every pass. As well, you can just set up the benchmark like this and avoid having perl eval a sub call in a string:

    timethese ( $count, { 'Method One S' => sub { my $data = 'foo-bar-baz'; $data =~ s/-/_/g; } 'Method Two TR' => sub { my $data = 'foo-bar-baz'; $data =~ tr/-/_/; } } );

    Not a big deal but it may save you some typing in the future. Your way has the benefit of being easy to run the subs once and test their output, tho...

    BTW, I moronically goofed my test string too... =)

    --
    $you = new YOU;
    honk() if $you->love(perl)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2014-08-30 01:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (290 votes), past polls