Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

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 all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2018-07-20 07:28 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (426 votes). Check out past polls.