3 ms·
I find your comparing the running times of qsort and std::sort a bit dubious. How sure are you that the difference in running time has anything at all to do wit
by OskarS 8y ago
I find your comparing the running times of qsort and std::sort a bit dubious. How sure are you that the difference in running time has anything at all to do with inlining, and not differences in how those two methods are implemented? I get the point you're making, that using templates opens up all sorts of possible inlining optimization possibilities, but it still seems like an unfounded comparison to say that the run time difference between those two are solely due to inlining.
- wahern 8y agoThere's no reason libc couldn't define a static inline implementation of qsort in the header. The callbacks could be inlined just like in C++. But putting tens of thousands of lines of basic library code into headers isn't commonly done in the C ecosystem for very good reasons--e.g. ability to fix a bug or improve performance[1] without having to recompile dependent binaries. That said, in the BSD universe there are linked-list and tree implementations provided in <sys/queue.h> and <sys/tree.h> that are almost universally used, in both the kernel and userland. The macro definitions might be uglier than templates but no less typesafe and nobody forces you to look at the headers. Unlike sorting and hash algorithms 1) these never change and 2) they make it possible to create intrusive (space-efficient, zero-allocation) data structures, so it just makes sense to provide them in headers. [1] qsort is not always implemented using quicksort. In fact, I think it's the exception rather than the norm.