http://qs321.pair.com?node_id=505164


in reply to assigning the maximum of two numbers

I think your benchmark is flawed. List::Util isn't as fast as you think it is. Go look at the source. And why are you looking at the speed of comparing two numbers?

If I find some time later, I'll elucidate on why your benchmark is flawed unless I'm preempted by some monk with more free time.

Update: Well, looks like everyone has done a good job saying the things that I first thought when I looked at your code. You're comparing apples to oranges. If you really want to benchmark "finding the max of 2 values", then you need to make sure that all of your subs do just that or if one needs to do more, then they all need to do the same extra stuff. In your subs, some copy the parameters, others don't; some use assignment, some don't; some use temp variables, some don't; etc.

Also, while you're using the same array for all benchmarks (which is good to keep the tests "the same"), in a real life situtation which number is larger will change with time, so to get numbers that are "more realistic", rather than using a random 2 element list, you should use 2 known lists. One where the first number is larger, and one where the second number is larger. Then either average the results or report them separately (depending on what you're trying to show)

As an aside, my initial reaction about List::Util is that "his numbers have to be wrong because I know List::Util is implemented in perl and uses the ternary op". It turns out that on the system I normally use (perl 5.8.6, List::Util 1.14) it's implemented in pure perl, but on another system I have (perl 5.8.0, List::Util 1.09_00) it's implemented in XS. In my benchmarks though, the straight ternary op is faster than List::Util::max even with the XS implementation. (I'd show my benchmarks, but having just read a thread on hop-discuss about how Benchmark.pm is broken I'm in the process of writing and using my own benchmarking routines, so take this result with a big grain of salt :-)

Replies are listed 'Best First'.
Re^2: assigning the maximum of two numbers
by bageler (Hermit) on Nov 02, 2005 at 22:57 UTC
    I was just wondering...is curiosity not a valid reason to do things anymore? I didn't want to make any assumptions. It also lead me to thinking I might want to grab the greatest of a larger list of numbers, let's say I change my array to search to have 100 values. Taking out the ternary, which doesn't quite apply in this new context, here are the results:
    Rate sort2 sort loop List sort2 3380/s -- -9% -81% -94% sort 3725/s 10% -- -79% -93% loop 17454/s 416% 369% -- -67% List 53227/s 1475% 1329% 205% --



    Lastly, it's a meditation. Food for thought. I'm not trying to cure cancer here but you never know.

    List::Util: you forgot to read the comments. the perl stuff is only compiled if the XS fails to load.

      Oh no, I didn't mean to imply that being curious was a bad thing. It's just that this is an odd thing to be curious about the speed of execution. By all means, be extra-curious :-)