Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: problems with tr///;

by Sigmund (Pilgrim)
on Sep 26, 2001 at 13:14 UTC ( [id://114763]=note: print w/replies, xml ) Need Help??


in reply to problems with tr///;

why use the translate operator?
couldn't you use the substitution operator, like:

$script =~ s/\\//g;

(the g meaning "globally", as to say "match each \ in string".
otherwise you could even use the numeric ascii code to specify a "\".

SiG

perl -le 's ssSss.s sSsSiss.s s$sSss.s s.$s\107ss.print'

Replies are listed 'Best First'.
Re: Re: problems with tr///;
by suaveant (Parson) on Sep 26, 2001 at 17:31 UTC
    Because the transliteration operator is more efficient... I don't know why, but I know that it is.

                    - Ant
                    - Some of my best work - Fish Dinner

      <voice type="marvin_the_martian"<Oohh, you have made me very skeptical</voice>

      So I tried a couple of benchmarks. (I'm using 5.6.1, build 626 from ActiveState.) Here's the code:

      use Benchmark; $var = '\a\\aa\\\aaa\\\\aaaa'x100; print "s: "; timethis(100000, '$var =~ s/\\\//g'); # 3 \ for benchmark print "tr: "; timethis(100000, '$var =~ tr/\\\//d');
      and here's the results
      s: timethis 100000: 1 wallclock secs ( 0.51 usr + 0.00 sys = 0.51 +CPU) @ 195694.72/s (n=100000) tr: timethis 100000: 2 wallclock secs ( 1.68 usr + 0.00 sys = 1.68 +CPU) @ 59453.03/s (n=100000)
      You can play with $var and see the effect on the timings. Maybe tr used to be faster, but that's no longer the case.

      <soapbox>
      It's really beside the point. The point is the right tool for the job. In this case the right tool is the substitution operator. If you wanted to make multiple single character changes in the string then tr is what you want. Note the difference between:

      $var =~ tr /ab/cd/; # and $var =~ s /ab/cd/g;
      </soapbox>

      Have fun,
      Carl Forde

        <voice type="marvin_the_martian">Greetings earth specimen. You have made a naughty Benchmark goof, so I shall have to blow you up</voice> ;-)

        Note that $var is modified by the subs, so calling them over and over again is pointless. This is very misleading because the "work" you are attempting to benchmark is only being done once. Your tests essentially behave like this:

        $var = 'abc'; $var =~ s/b//; # $var is now 'ac' $var =~ s/b//; # still 'ac' even before the s/// $var =~ s/b//; # yet another attempt to remove 'b' from 'ac' $var =~ s/b//; # well, you get the point.... .... etc.
        Here is a better benchmark (on 5.6.0) that restores my faith in tr///:
        #!/usr/bin/perl -wT use strict; use Benchmark; our $var; $var = '\a\\aa\\\aaa\\\\aaaa'x100; ## rerun the original benchmarks to provide a baseline timethese(100000, { 'old s' => sub { $var =~ s/\\//g }, 'old tr' => sub { $var =~ tr/\\//d }, }); ## try some new benchmarks that reinitialize $var each time timethese(50000, { 'new s' => sub { $var = '\a\\aa\\\aaa\\\\aaaa'x100; $var =~ s/\\/ +/g }, 'new tr' => sub { $var = '\a\\aa\\\aaa\\\\aaaa'x100; $var =~ tr/\\ +//d }, }); =OUTPUT Benchmark: timing 100000 iterations of old s, old tr... old s: 1 wallclock secs ( 0.56 usr + 0.00 sys = 0.56 CPU) @ 17 +8571.43/s (n=100000) old tr: 2 wallclock secs ( 1.23 usr + 0.00 sys = 1.23 CPU) @ 81 +300.81/s (n=100000) Benchmark: timing 50000 iterations of new s, new tr... new s: 43 wallclock secs (30.81 usr + 0.02 sys = 30.83 CPU) @ 16 +21.80/s (n=50000) new tr: 3 wallclock secs ( 2.06 usr + 0.00 sys = 2.06 CPU) @ 24 +271.84/s (n=50000)

        -Blake

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-04-26 00:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found