|Keep It Simple, Stupid|
Re: The High Price of Golf, and A Surpriseby bart (Canon)
|on Sep 07, 2002 at 09:09 UTC||Need Help??|
You should include cmp in your benchmark. I think you'll find it slower than the built-in sort. That speed difference is the overhead of the callback sub.
I expect that cmp in general is a far slower operation than <=>. The latter only takes one CPU instruction (plus the overhead of the interpreter), the former is a slow library call for many strings — especially if the strings you compare are identical, because now all characters in the strings have to be examined.
And I think what you found is that the callback overhead for sort is still faster than the speed difference between these two ops.
Enough blah blah. I've added them to your benchmark code, so it now looks like:
I'll update with the results in a moment. This benchmark takes many minutes to run, and I don't want to skew the results by doing heavy stuff with my humble PC.
Update: I'm back. Here are the results:
That's odd. there is NO speed difference between [fi]_cmp and [fi]_alpha. That means that the cost of the callback is negligable... unless Perl is optimizing the callback away?
Just to make sure, I've also swapped $a and $b in my callback sub. It doesn't make a difference.
Conclusion: the speed gain you get by using numerical sort, is entirely due to the speed difference between the ops <=> and cmp.