Hacker News new | ask | show | jobs
by dorinlazar 1651 days ago
C++ is just a stricter C - if you write proper C, it will be a correct C++ program, therefore it would run at the same speed. There's no way C can be other than marginally faster than C++ - if you write C and compile it with a C++ compiler.

Then you have various optimizations that come from things like templates, constexpr. Look at the qsort method - to have a modicum of genericity you need to pass a function that will be invoked. Instead, a C++ version would be able to be optimized to compare the elements directly, without calling a function, avoiding a big number of function calls.

There's no way to make C faster than well written C++. But C++ is complex and a lot of people write bad C++, that's why it has developed a bad reputation. It's really undeserved.

1 comments

> There's no way C can be other than marginally faster than C++

In theory `restrict` (to disallow pointer aliasing like in Fortran) could lead to optimization gains, same as not having exceptions (the possibility of exceptions prohibits certain optimizations of the C++ compiler).

I agree; of course, both clang and gcc extend C++ with __restrict__ if I remember correctly; from what I know, there are more things that can be done in C++ to improve over C than the other way around.
IIRC, __restrict__ in clang and g++ does nothing when you write C++ code. It is there just for compatibility with C.