Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
<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


In reply to Re: Re: Re: problems with tr///; by cforde
in thread problems with tr///; by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2024-04-23 16:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found