Re: Generally, are the programs written by C++ slower than written by C 10% ?
BGB <cr88192@hotmail.com> wrote:
"Nobody uses qsort() anyways" is just the epitome of this. Contrast it
with std::sort(), which is actually a really decent general use sorting
function, and in the vast majority of cases it's pretty useful, and it's
very commonly used in C++ in most situations where sorting is needed.
Moreover, it's very easy to use (much easier than qsort()).
however, avoiding writing little bits of code is not nearly so high on
many peoples' priority list.
Implementing a fast comparison-based sort function which is efficient
with all possible inputs isn't just a "little bit of code". It's actually
pretty complicated. (Quicksort is a relatively short algorithm, although
not many people can implement correctly it on the spot, without looking
somewhere the details. However, quicksort has worst-case scenarios that
make it extremely inefficient. To make it efficient in all cases you need
to use optimizations which are much more laborious to implement. Or use
some other sorting algorithm, which will either be slower than quicksort
(such as heap sort) or consume a lot more memory (such as merge sort).)
Doing a binary search on a sorted array, while extremely small piece
of code, is surprisingly hard to get correct on-the-spot. I remember a
study that showed that something like over half of experienced professional
programmers implemented it incorrectly when they couldn't look for a
reference. (Off-by-one errors can be pretty annoying.)
When you need a more complex algorithm or data container, the amount
of work can be daunting. For example implementing a balanced binary tree
is far from trivial.
Anyways, why should one have to write algorithms always from scratch?
If I want to sort an array, I don't want to write a sorting algorithm each
time, no matter if it takes just three lines of code. There's no need.
There *shouldn't* be no need.
In fact, it's not just a question of convenience. It's a question of
code correctness. When the algorithm implementation has been tested for
years by millions of people, you can be pretty certain that it's correct.
If you implement it yourself, there's a good chance that there will be a
bug that might not be immediately apparent, and might kick in unexpectedly.