Flame has asked for the wisdom of the Perl Monks concerning the following question:

I tried a quick search for this, but I wasn't really able to find anything. I don't know if it's because I couldn't find a good keyword to search for, or it hasn't been asked before.

Anyway, can someone tell me which would be faster for replacing a single charater with another single character?

Eg: s/\./\//go vs tr/\./\// (replacing . with /)

Thanks to anyone that can help. I've only used tr once before, so I really know none of it's virtues compared to s, except for it's ability to accept a list of characters...


"Wierd things happen, get used to it"

Flame ~ Lead Programmer: GMS
http://gms.uoe.org

Replies are listed 'Best First'.
(crazyinsomniac) Re: tr/// vs s/// for single character
by crazyinsomniac (Prior) on Oct 07, 2001 at 23:39 UTC
      Well, that answers that, I suspected it had been asked before, I just didn't try the right search... heh.

      Thanks


      "Wierd things happen, get used to it"

      Flame ~ Lead Programmer: GMS
      http://gms.uoe.org
Re: tr/// vs s/// for single character
by Zaxo (Archbishop) on Oct 07, 2001 at 23:41 UTC

    For single character replacement, tr/// is the ticket. It comes without the overhead of the regex engine.

    One way to answer questions like this is use Benchmark;

    After Compline,
    Zaxo

Re: tr/// vs s/// for single character
by strfry() (Monk) on Oct 08, 2001 at 19:20 UTC
    unrelated to the question, but useful nonetheless: you can use an alternate delimiter to make your substitutions and transliterations easier to read. eg: "y/\./\//" might be easier read as "y#\.#/#"
    have fun
    strfry()
Re: tr/// vs s/// for single character
by John M. Dlugosz (Monsignor) on Oct 08, 2001 at 09:13 UTC
    Funny, nobody mentioned using the benchmarking module. (people are often surprised at the actual results)
      Hmmm time to get out the special can-o-worms tin opener. Isn't there always time for another discussion about (premature) optimization and the many ways in which honest but unwary programmers may be misled by benchmarks? :)

      Scratch that. It's been done too many time before. The question "Which is faster, X or Y?" is badly formed, and particularly silly (sorry, but it is) when it comes to tr/// vs s/// for a one character substitution. The question should always be "Which fits best in this particular place in my program?" The latter question is much more to the point. In most cases the time difference will be negligible.

      Do not be overly concerned about fine-tuning Perl programs for execution speed. You will end up sacrificing your time and the clarity of your code for little tangible reward.
        I agree, the proper question is "which is better in my program". Speed may be one of several concerns.
        In most situations I would agree.... However, in this case the problem space was clearly defined and the question is a valid one in my opinion. The question was something like:

          "I want to replace all occurances of '.' with '/' in a string. I know that there are at least two ways to do this (s/// and tr///). Which one is faster?"

        Now perhaps one could argue that "faster" should be replaced with something a bit more encompassing (better, 'the right choice', etc), but in the end what really is the difference between s|\.|/|g and tr|\.|/|?

        What if the question had been:

          "I'd like to grab the last character in a string. I know there are at least two ways to do this (split '',$txt)[-1] and substr($txt,-1). Which one is faster?"

        Would it still be considered premature optimization to suggest that substr is the correct answer?

        In both questions there is a solution that uses a slower general tool (regex, split) and one with a faster specialized tool (tr///, substr). If you aren't going to use the specialized tools in these cases, when are you *ever* going to use them?

        Oh, and I posted some benchmarks a while back showing tr/// running about 15 times faster that s/// for a particular case...

        Update: virtualsue has brought to my attention that her "premature optimization" statement was about running benchmarks for small insignificant parts of a program. She wasn't advocating s/// over tr/// in this case, simply stating that benchmarking the two was a poor use of human cycles. To that I wholehartedly agree... I seem to have read the node slightly out of context, and get a little touchy when I see people using elephant guns (s///) when a well designed flyswatter (tr///) would do.

        -Blake