Hacker News new | ask | show | jobs
by emmelaich 3360 days ago
qsort is both unsafer and slower than a good templated sort.
1 comments

There are indeed trade-offs. But the comment I was replying to implied that you couldn't write a generic sort for all comparable types. That is wrong.

Unsafer yes.

But it's only slower because it's not being inlined. If the compiler can see both the implementation of qsort and the comparison function, there is no reason why it would be slower than a templated sort. You could accomplish this with link-time optimisation or by moving the implementation of qsort to a header file.

A trade-off you didn't mention: templates will cause multiple copies of the sort to be inlined in the executable. This can lead to bigger executables (with slower start-up) and more cache misses.

Another trade-off: templates lead to much slower compile times.