Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re: Re: Re: Re: string manipulation

by Desdinova (Friar)
on Mar 30, 2001 at 03:57 UTC ( #68244=note: print w/replies, xml ) Need Help??

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

You have a valid point in that node i was kind of comparing apples to oranges (Of course that node was about uppercasing input which is a bit different). I must have been having brain dead kind of day. As for your number I changed my benchmark code to this for this specific case to this:
#!/usr/local/bin/perl -w use strict; use Benchmark; my $count = 900000; ## Method number two sub One { my $data='for-bar-baz'; $data =~tr/-/_/; } ## Method number Two sub Two { my $data='for-bar-baz'; $data =~s/-/_/g; } ## We'll test each one, with simple labels timethese ( $count, {'Method One TR' => '&One', 'Method Two s'=> '&Two' } ); exit;
Which results in the following
Benchmark: timing 500000 iterations of Method One TR, Method Two s... Method One TR: 2 wallclock secs ( 1.87 usr + 0.00 sys = 1.87 CPU) @ + 267379.68/s (n=500000) Method Two s: 5 wallclock secs ( 4.84 usr + 0.00 sys = 4.84 CPU) @ +103305.79/s (n=500000)
FYI- I got these numbers using Perl 5.6.0 on Win32 You are right that this is not as big of difference as 17:1 (Which when I first ran seemed odd to me...) But is still a decent gap.
I got the suggestion about not using S when TR will do from Effective Perl Programming (co-authored by our own merlyn). It is a great book with lots of info tweaking your code.

off to update that other node now...
PS- your benchmark doesnt do anything in either case the line:my $data='for bar baz'; Should be changed to have the char being looked for. Like So: my $data='for-bar-baz';

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://68244]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2018-01-16 07:56 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (175 votes). Check out past polls.