Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Idiomatic optimizations

by BlueLines (Hermit)
on May 01, 2002 at 00:37 UTC ( [id://163195]=note: print w/replies, xml ) Need Help??


in reply to Idiomatic optimizations

If you really want to nitpick, try using single quotes instead of double quotes around static text:
#!/usr/bin/perl -w use Benchmark qw(cmpthese); cmpthese (10000000, { single => sub { $foo = 'foo'}, double => sub { $foo = "foo"} });
gives me:
[jon@valium jon]$ ./test.pl Benchmark: timing 10000000 iterations of double, single... double: 1 wallclock secs ( 1.91 usr + 0.00 sys = 1.91 CPU) @ 52 +35602.09/s (n=10000000) single: 2 wallclock secs ( 1.25 usr + 0.00 sys = 1.25 CPU) @ 80 +00000.00/s (n=10000000) Rate double single double 5235602/s -- -35% single 8000000/s 53% --
50% improvement when strings aren't interpolated.

BlueLines

Disclaimer: This post may contain inaccurate information, be habit forming, cause atomic warfare between peaceful countries, speed up male pattern baldness, interfere with your cable reception, exile you from certain third world countries, ruin your marriage, and generally spoil your day. No batteries included, no strings attached, your mileage may vary.

Replies are listed 'Best First'.
Re: Re: Idiomatic optimizations
by sfink (Deacon) on May 01, 2002 at 01:55 UTC
    What perl are you using? Those should be identical at runtime, since interpolation is converted to concatenation by the tokenizer. For evidence, try:
    % perl -MO=Deparse -e '$foo=q(foo)' $foo = 'foo'; -e syntax OK % perl -MO=Deparse -e '$foo=qq(foo)' $foo = 'foo'; -e syntax OK
    I ran your benchmark program, and got +/- 3%. Try running it a few times.
      I was logged in remotely to my home machine when i ran this, which means that xscreensaver was running with 100% of my cpu (yay OpenGL). I ran the test again without xscreensaver and i got the same results you did. oh well.

      BlueLines

      Disclaimer: This post may contain inaccurate information, be habit forming, cause atomic warfare between peaceful countries, speed up male pattern baldness, interfere with your cable reception, exile you from certain third world countries, ruin your marriage, and generally spoil your day. No batteries included, no strings attached, your mileage may vary.
      I wonder if the results have anything to do with you using q instead of a single quote and qq instead of double quotes.
Re: Re: Idiomatic optimizations
by samtregar (Abbot) on May 01, 2002 at 03:22 UTC
    That is very odd. I get absolutely no difference with Perl 5.6.1:
    Rate single double single 3937008/s -- 0% double 3937008/s 0% --

    -sam

Re: Re: Idiomatic optimizations
by belg4mit (Prior) on May 01, 2002 at 03:50 UTC
    With 5.6.1...

    On an otherwise unloaded P4 1.g GHz

    1 Rate single double 2 Rate single double single 4219409/s -- -7% single 4273504/s -- -18% double 4524887/s 7% -- double 5208333/s 22% -- 3 Rate single double 4 Rate single double single 4201681/s -- -8% single 4166667/s -- -22% double 4566210/s 9% -- double 5347594/s 28% --

    On an otherwise reasonably loaded (0.31) sun4u

    1 Rate double single 2 Rate double single double 3079766/s -- -18% double 3054368/s -- -11% single 3763643/s 22% -- single 3419973/s 12% -- 3 Rate double single 4 Rate double single double 2866972/s -- -18% double 3107520/s -- -10% single 3495281/s 22% -- single 3437607/s 11% --

    --
    perl -pew "s/\b;([mnst])/'$1/g"

      On an otherwise unloaded P4 1.g GHz

      For certain very specific meanings of the word "unloaded", I'm sure. "foo" is optimized to 'foo' at compile time, so neither of them can possibly be faster. As you can see, the generated bytecode is equivalent:

      2;0 juerd@ouranos:~$ perl -MO=Concise -e'$foo = "foo"' 6 <@> leave[t1] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 1 -e:1) v ->3 5 <2> sassign vKS/2 ->6 3 <$> const(PV "foo") s ->4 - <1> ex-rv2sv sKRM*/1 ->5 4 <$> gvsv(*foo) s ->5 -e syntax OK 2;0 juerd@ouranos:~$ perl -MO=Concise -e'$foo = '\''bar'\' 6 <@> leave[t1] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 1 -e:1) v ->3 5 <2> sassign vKS/2 ->6 3 <$> const(PV "bar") s ->4 - <1> ex-rv2sv sKRM*/1 ->5 4 <$> gvsv(*foo) s ->5 -e syntax OK
      Even "Hello, $name!" is optimized to 'Hello, ' . $name . '!'.

      Note: I _do_ think people should use single quotes when not interpolating. It saves you a lot of hits on the backslash key, and avoids getting bitten by: "$You're kidding me" (eq. "$You::re kidding me"), "my@emailaddress.com" (intended: "my\@emailaddress.com").

      - Yes, I reinvent wheels.
      - Spam: Visit eurotraQ.
      

        >For certain very specific meanings of the word "unloaded"
        Yes of course, namely 0.0 (!) And we've already established that the bytecode is *supposedly* the same, afterall is it not true that Deparse is not guaranteed to reconstruct things exactly? So this adds nothing new. It does not help describe the erratic behavior.

        --
        perl -pew "s/\b;([mnst])/'$1/g"

Log In?
Username:
Password:

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

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

    No recent polls found