in reply to Re: Optimizing Perl Code - single versus double quotes really that important?
in thread Optimizing Perl Code - single versus double quotes really that important?

Speed-wise, I don't think it makes much difference

Speedwise, it makes no difference. They produce exactly the same code.

$ perl -MO=Concise,-exec -e'$x = qq{a $b c}' 1 <0> enter 2 <;> nextstate(main 1 -e:1) v 3 <$> const[PV "a "] s 4 <#> gvsv[*b] s 5 <2> concat[t3] sK/2 6 <$> const[PV " c"] s 7 <2> concat[t4] sKS/2 8 <#> gvsv[*x] s 9 <2> sassign vKS/2 a <@> leave[1 ref] vKP/REFC -e syntax OK $ perl -MO=Concise,-exec -e'$x = q{a }.$b.q{ c}' 1 <0> enter 2 <;> nextstate(main 1 -e:1) v 3 <$> const[PV "a "] s 4 <#> gvsv[*b] s 5 <2> concat[t3] sK/2 6 <$> const[PV " c"] s 7 <2> concat[t4] sKS/2 8 <#> gvsv[*x] s 9 <2> sassign vKS/2 a <@> leave[1 ref] vKP/REFC -e syntax OK
  • Comment on Re^2: Optimizing Perl Code - single versus double quotes really that important?
  • Download Code

Replies are listed 'Best First'.
Re^3: Optimizing Perl Code - single versus double quotes really that important?
by DStaal (Chaplain) on Feb 05, 2009 at 18:21 UTC

    Well...

    perl -MO=Concise,-exec -e '$x=qq{a $b c}' 1 <0> enter 2 <;> nextstate(main 1 -e:1) v:{ 3 <$> const[PV "a "] s 4 <#> gvsv[*b] s 5 <2> concat[t3] sK/2 6 <$> const[PV " c"] s 7 <2> concat[t4] sKS/2 8 <#> gvsv[*x] s 9 <2> sassign vKS/2 a <@> leave[1 ref] vKP/REFC -e syntax OK perl -MO=Concise,-exec -e '$x=q{a $b c}' 1 <0> enter 2 <;> nextstate(main 1 -e:1) v:{ 3 <$> const[PV "a $b c"] s 4 <#> gvsv[*x] s 5 <2> sassign vKS/2 6 <@> leave[1 ref] vKP/REFC -e syntax OK

    So obviously there is some minor difference at compile-time. (Which would be the only difference I expect.) Once compiled equivalent expressions produce equivalent op-code, it's just that the compile process isn't exactly the same.

    Unlikely to be noticeable though, and really irrelevant to my original reply.

      Apples and oranges. Those snippets don't build the same string. Of course they result in different code.

        Which was my point: The compiler looks at it differently. So there is a difference at compile time, as the compiler decides whether or not it needs to interpolate. I don't know how much time that takes. (If any: It is possible that both code-paths are equivalent in time, after all.)

        In theory that could be a major hit at startup. I doubt it however. And it depends on the situation: In the current context we are talking about a script that is being used in some CGI environment, so if the hit was really bad it is likely the script could be run under mod_perl, which would remove it from consideration. (If it isn't already.)