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

Greetings, Fellow Monks.

Recently, I've been doing a lot of work in languages other than Perl. Most of them don't care whether I use single or double quotes. I know that Perl will interpolate variables inside double quoted statements, and won't inside single quoted statements. However, I heard a rumor(I can't remember where) that using double quoted statements when there isn't a variable inside will slow down my Perl script. Does anyone know if this is true?



Thanks,
Spidy

Edit: s/concatenate/interpolate/;

Replies are listed 'Best First'.
Re: Single Quotes Versus Double Quotes
by ikegami (Patriarch) on Oct 12, 2006 at 16:15 UTC

    The Perl compiler produces the same opcodes for both types of strings.

    >perl -MO=Concise -e "$a = 'abc'"; 6 <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 1 -e:1) v ->3 5 <2> sassign vKS/2 ->6 3 <$> const[PV "abc"] s ->4 - <1> ex-rv2sv sKRM*/1 ->5 4 <#> gvsv[*a] s ->5 -e syntax OK >perl -MO=Concise -e "$a = qq{abc}"; 6 <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 1 -e:1) v ->3 5 <2> sassign vKS/2 ->6 3 <$> const[PV "abc"] s ->4 - <1> ex-rv2sv sKRM*/1 ->5 4 <#> gvsv[*a] s ->5 -e syntax OK

    The only difference would be in compile time, and the difference should be infinitesimal.

Re: Single Quotes Versus Double Quotes
by davorg (Chancellor) on Oct 12, 2006 at 16:16 UTC

    It's not true as Perl converts the unnecessary double quotes to single quotes as it compiles your code. Of course, you could argue that this conversion takes time, but it's going to be negliable.

    But I always make a point of using single quotes when that's what is needed. Not for the compiler, but for my maintenance programmer (who might very well be me in a few months time). In my opinion, putting double quotes around a string is a single to the programmer that "something" is going on in that string and that you need to be a bit more careful when changing the contents of that string.

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      Future maintainance work is exactly the reason I use double quotes. I find it not to be uncommon that I later want to add a variable in a string to be printed. Having to change to quotes is work I try to avoid.

      And programmers that get confused when encountering double quotes, but nothing to interpolate. Well, sorry, but if that confuses you, you shouldn't be programming at all. Or at least, not in Perl.

        I'm not saying that it confuses me. I'm saying that I use it as a flag to myself (or whoever is editing my code). Single quotes denote a string with no variable interpolation or special escape sequences. It seems obvious and useful to me.

        And, yes, I sometimes have to change quote characters when I'm editing code. I see that as part of keeping the code as readable as possible.

        I'm glad to see that Damian Conway agrees with me (see section 4.1 of Perl Best Practices).

        --
        <http://dave.org.uk>

        "The first rule of Perl club is you do not talk about Perl club."
        -- Chip Salzenberg

Re: Single Quotes Versus Double Quotes
by kwaping (Priest) on Oct 12, 2006 at 16:15 UTC
    I can understand that thinking, but I don't think it's correct. It's my understanding that Perl will treat a double-quoted string that contains no variables as a single-quoted string. Example:
    perl -MO=Deparse -e 'print "hi"' print 'hi'; -e syntax OK
    Also, the word you were thinking of is "interpolate", not "concatenate". :)

    ---
    It's all fine and dandy until someone has to look at the code.

      Also, the word you were thinking of is "interpolate", not "concatenate". :)

      Aye. Even though interpolation is implemented using concatenation, "strings are concatenated" while "variables are interpolated into strings".

      >perl -MO=Concise -e "$a = qq{$b$c}"; 8 <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 1 -e:1) v ->3 7 <2> sassign vKS/2 ->8 - <1> ex-stringify sK/1 ->6 - <0> ex-pushmark s ->3 5 <2> concat[t4] sK/2 ->6 <---- - <1> ex-rv2sv sK/1 ->4 3 <#> gvsv[*b] s ->4 - <1> ex-rv2sv sK/1 ->5 4 <#> gvsv[*c] s ->5 - <1> ex-rv2sv sKRM*/1 ->7 6 <#> gvsv[*a] s ->7 -e syntax OK
Re: Single Quotes Versus Double Quotes
by grep (Monsignor) on Oct 12, 2006 at 16:22 UTC
    Everyone seems to be right. The difference is tiny.
    use strict; use warnings; use Benchmark ('cmpthese'); cmpthese(1000000, { double => \&dq, single => \&sq, none => \&nq }); sub dq { my $str = "a" x 100; my $new_val = "$str"; } sub sq { my $str = "a" x 100; my $new_val = '$str'; } sub nq { my $str = "a" x 100; my $new_val = $str; }
    Produces
    Rate double single none double 1030928/s -- -10% -15% single 1149425/s 11% -- -6% none 1219512/s 18% 6% --


    grep
    One dead unjugged rabbit fish later
      You're comparing assinging a string of 4 characters to assigning a string of 100 characters. I don't see the relevance to the topic at hand. Here's a more relevant benchmarks.
      Compile-time difference Rate single double single 30027/s -- -0% double 30160/s 0% -- Run-time difference Rate single double single 5968932/s -- -13% double 6893402/s 15% -- Rate double single double 6218672/s -- -12% single 7062848/s 14% --

      I'm getting lots of variation in the run-time results. The differences are probably due to external sources. Since the opcodes are the same, they should take the same amount of time.

      use strict; use warnings; use Benchmark qw( cmpthese ); my $as = 'a' x 100; sub c_dq { eval "\$a = \"$as\";"; } sub c_sq { eval "\$a = '$as';"; } eval "sub r_dq { \$a = \"$as\"; }"; eval "sub r_sq { \$a = '$as'; }"; print("Compile-time difference\n"); cmpthese(-3, { double => \&c_dq, single => \&c_sq, }); print("Run-time difference\n"); cmpthese(-3, { double => \&r_dq, single => \&r_sq, });
Re: Single Quotes Versus Double Quotes
by Melly (Chaplain) on Oct 12, 2006 at 16:17 UTC

    Well, a bit of benchmarking might settle the case - however my guess would be "yes".

    I would assume that double-quoted strings need checking for variables - however, if this takes place during the compile stage (and double-quoted strings w/o any vars are optimised out), then benchmarking might not show the difference (since the extra time would be used in the compile stage, rather than the run stage).

    Sorry for the ignorant answer, but I was curious to see whether some other monk would confirm or shoot down my assumption.

    <update>Heh - looks like my assumption was correct...</update>

    Tom Melly, tom@tomandlu.co.uk