Pregunta

I'm going through a List<>, performing some operations on each item, and then depending on the outcome of those operations, potentially adding each one to another data structure, for which I'm currently using a SortedSet<>. After this, I want the top-sorted n items as a list.

The only other thing I ever have to do to this SortedSet<> is clear the whole thing and start anew. Is there any way I can milk a little more performance out of this?

I saw this similar question where the poster was able to achieve about a 1/6 reduction in run time using a custom red-black tree (after their question was answered). But isn't the SortedSet<> a red-black tree already? Is it worth it for me to try and bank some performance increase through creating my own data structure in this case?

¿Fue útil?

Solución

You can try getting a little better performance by not "paying" for what your program does not use: rather than using a tree structure, which keeps all elements sorted, you can use a heap which does not sort elements, but lets you extract k largest elements in k * log(n) time.

Heaps tend to be faster than balanced trees on insertions and deletions, and they also occupy contiguous regions of memory, which may improve "cache friendliness" in some cases. Of course the speed-up is not free: heaps do not support fast sorting or removal of items other than the ones at the top.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top