Re: std::string::data()
thant wrote:
:: On Jun 5, 8:41 am, "Stephen Howe"
:: <sjhoweATdialDOTpipexDOT...@giganews.com> wrote:
::::: http://www.research.att.com/~bs/new_learning.pdf
:::
:::: C++ is not inherently faster than C.
:::
::: Not inhererently, no. But in certain areas, it is faster than C.
::: std::sort() can be (and often is), faster than qsort(). Why? -
::: Because the compiler can inline the < operator or equivalent.
::: qsort() will always be calling through a function pointer.
::
:: qsort is not C anymore than std::string is C++.
::
:: You could write a version of qsort that only applied to a specific
:: datatype using a specific (and inlined) version of the < operator.
:: You could also write a macro function that inlined a specific
:: ordering function.
::
:: The advantage that C++ has over C is that it allows you to do these
:: things more conveniently and with more confidence that the code is
:: type safe. This is certainly nothing to sneeze at, but the
:: unqualified claim that C++ is somehow sometimes faster than C is
:: still utter nonsense.
The advantage C++ has here is that there already is a library function
std::sort, that is typesafe and tested and generic and customizable,
and it can still inline the function and any custom compare functor
you provide.
It runs faster than C's supposedly highly optimized library function.
And the C library provider cannot do anything about it, because the C
language has fewer high level features than C++.
I am sure you can write a hand optimized qsort for you specific data
type. It might even run as fast as std::sort! But you will have to do
it each time you need to sort a new type. With C++, the compiler does
it all for you.
Bo Persson
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]