Syntactic Confectionery Delight | |
PerlMonks |
Re: A heap of medians: efficiency vs. speedby thospel (Hermit) |
on Jun 11, 2008 at 13:58 UTC ( [id://691466]=note: print w/replies, xml ) | Need Help?? |
As author of Heap::Simple I'd like to add the following observations:
You can also add the dirty option. Heap::Simple::XS will then use direct numeric compares instead of calling perl's >. This breaks overload and ties though. That way the Heap::Simple solution is about twice as fast as sort. The break-even is somewhere in the ten thousands of elements. The resulting code looks something like this:
This doesn't change the fact that for even quite big lists sort is just faster for this kind of problem. Heaps are mainly for when you need to know the smallest element at many points in time while the set of elements keeps changing. Or indeed for some cases where compare is very expensive, but even then there are better ways to do it with less compares if you just want the median. PS: Heap::Simple already uses special code to make an insert of many elements at once faster than repeatedly inserting one element. You seem to hint at an even faster algorithm but I don't see how to use Tarjan's algorithm for that. Can you give me a reference ?
In Section
Meditations
|
|