Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re^2: code optimization

by JavaFan (Canon)
on Nov 03, 2011 at 12:28 UTC ( #935646=note: print w/replies, xml ) Need Help??

in reply to Re: code optimization
in thread code optimization

do not compute the division several times.
That's debatable. The trade-off is, arivu calculates the division only a second time if it's actually smaller than the previous minimum value. You store the result in a variable *all the time*. Your benchmark is biased in your favour because Perl actually keeps the variable skeleton around for each iteration of the benchmark. You're also using split instead of pattern with backreferences. If both "arivu" and "choroba" use split, the smallest ratio is in the beginning of the list, and the benchmark uses strings instead of subs (to avoid costs being amortized over runs), arivu's solution is actually marginally faster. (On my machine, and by 5% +/- 6%).

So, what can we conclude?

  • Benchmarking is harder than you think.
  • The real gain is in the split vs backref pattern.
  • Doing a "low cost" operation all the time isn't always faster than doing a slightly "less lower cost" sometimes.

Replies are listed 'Best First'.
Re^3: code optimization
by choroba (Bishop) on Nov 03, 2011 at 12:31 UTC
    Interesting. I actually forgot to mention split in my comment, thanks for pointing it up.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (8)
As of 2018-10-17 20:28 GMT
Find Nodes?
    Voting Booth?
    When I need money for a bigger acquisition, I usually ...

    Results (97 votes). Check out past polls.