Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Faster Perl, Good and Bad News

by MrNobo1024 (Hermit)
on Aug 09, 2002 at 21:25 UTC ( #189056=note: print w/ replies, xml ) Need Help??


in reply to Faster Perl, Good and Bad News

"Premature optimization is the root of all evil" - Donald Knuth

You shouldn't go through contortions to make your program run a bit faster if it's not needed in the first place. It's just wasted effort, and will make your program harder to read and maintain.

I'm not saying you should never optimize, if a part of a program really *is* running significantly slowly, then by all means it should be sped up. But if you don't even check if it's needed, you could be wasting time optimizing, and making it more obfuscated in the process.

For example, check out these two ways to swap two variables: (Actually they're not exactly the same, for the first one to work they must be integers or same-length strings)

$x ^= $y ^= $x ^= $y; ($x, $y) = ($y, $x);
The first one is a bit faster, but if I didn't just say what it did (or if you had already seen it before) it would probably take a while to figure it out. The second one, it's obvious (if you know Perl) what it's doing, and unless it's going to be called 1000s of times it won't make any noticible speed difference.

--MrNobo1024
s]]HrLfbfe|EbBibmv]e|s}w}ciZx^RYhL}e^print


Comment on Re: Faster Perl, Good and Bad News
Download Code
Re: Re: Faster Perl, Good and Bad News
by sauoq (Abbot) on Aug 09, 2002 at 22:30 UTC
    For example, check out these two ways to swap two variables:
    $x ^= $y ^= $x ^= $y; ($x, $y) = ($y, $x);

    It might surprise you to learn that these aren't even equivalent. The XORs won't work with references.

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: Re: Faster Perl, Good and Bad News
by demerphq (Chancellor) on Aug 09, 2002 at 23:53 UTC
    Er by far faster and easier to read to someone inexperienced in perl is
    my $tmp=$x; $x=$y; $y=$tmp;
    Premature optimization is evil, but then again so is false lazyness

    Yves / DeMerphq
    ---
    Software Engineering is Programming when you can't. -- E. W. Dijkstra (RIP)

      I benchmarked them, there was no significant difference between list assignment and using a temp var, and IMO the list assignment is more readable. Still, TMTOWTDI, and the temp var isn't really *that* obfuscated...

      --MrNobo1024
      s]]HrLfbfe|EbBibmv]e|s}w}ciZx^RYhL}e^print

        Im curious what you call signifigant, and also how you benchmarked them. Please post your benchmark. In my tests, with a predeclared swap variable, I see a %50 difference when using dynamic variables, and a %25 difference when using lexical variables. Personally I call that signifigant, especially if its inside a loop. Oh the difference would probably be even more drammatic for lexicals but for the fact of having to repeatedly declare them, which wouldnt be necessary in a more realisitic situation.

        use Benchmark 'cmpthese'; our $a=1; our $b=2; our $tmp; print "Dynamic variables, swap benchmark.\n"; cmpthese -10, {standard => '$::tmp=$::a; $::a=$::b; $::b=$tmp;', list => '($::a,$::b)=($::b,$::a)' }; print "Lexical variables, swap benchmark.\n"; cmpthese -10, { standard_my => sub{my $x=1;my $y=2; my $z =$x; $x=$y +; $y=$z}, standard_local => sub{my $x=1;my $y=2; local $_=$x; $x= +$y; $y=$_}, standard_global => sub{my $x=1;my $y=2; $_=$x; $x=$y; $y +=$_}, list => sub{my $x=1;my $y=2; ($x,$y)=($y,$x)} +, }; __END__ Dynamic variables, swap benchmark. Benchmark: running list, standard, each for at least 10 CPU seconds... list: 11 wallclock secs (10.39 usr + 0.00 sys = 10.39 CPU) @ 66 +4534.94/s (n=6904518) standard: 11 wallclock secs (10.84 usr + 0.00 sys = 10.84 CPU) @ 98 +3324.54/s (n=10662188) Rate list standard list 664535/s -- -32% standard 983325/s 48% -- Lexical variables, swap benchmark. Benchmark: running list, standard_global, standard_local, standard_my, + each for at least 10 CPU seconds... list: 10 wallclock secs (10.13 usr + 0.00 sys = 10.13 CPU) @ 40 +4027.75/s (n=4090781) standard_global: 9 wallclock secs (10.05 usr + 0.00 sys = 10.05 CPU) @ 484686.67/s ( +n=4869647) standard_local: 10 wallclock secs (10.03 usr + 0.00 sys = 10.03 CPU) @ 402621.17/s ( +n=4038693) standard_my: 10 wallclock secs (10.80 usr + 0.00 sys = 10.80 CPU) @ 503630.27/s ( +n=5437696) Rate standard_local list standard_global s +tandard_my standard_local 402621/s -- -0% -17% + -20% list 404028/s 0% -- -17% + -20% standard_global 484687/s 20% 20% -- + -4% standard_my 503630/s 25% 25% 4% + --

        Yves / DeMerphq
        ---
        Software Engineering is Programming when you can't. -- E. W. Dijkstra (RIP)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (5)
As of 2014-07-12 09:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (239 votes), past polls