News for hackers. Stuff that matters.
Re:Which programming language or compiler is faster by adamddev1 on Saturday December 18, 2021 @01:33PM (#29607091)
Can anyone explain why C++ beats out C so clearly?
Re:Which programming language or compiler is faster by sdfdf4434r34r on Saturday December 18, 2021 @05:40PM (#29609308)
Re:Which programming language or compiler is faster by dorinlazar on Saturday December 18, 2021 @03:00PM (#29607991)
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.

Re:Which programming language or compiler is faster by ReleaseCandidat on Saturday December 18, 2021 @03:07PM (#29608046)
> 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).

Re:Which programming language or compiler is faster by dorinlazar on Saturday December 18, 2021 @03:37PM (#29608275)
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.
Re:Which programming language or compiler is faster by khoobid_shoma on Saturday December 18, 2021 @09:05PM (#29610692)
IIRC, __restrict__ in clang and g++ does nothing when you write C++ code. It is there just for compatibility with C.
Re:Which programming language or compiler is faster by khoobid_shoma on Saturday December 18, 2021 @02:37PM (#29607800)
I think the C code in the benchmarks are not really efficient. An experienced C programmer does not simulate C++ style memory management if speed matters.
Re:Which programming language or compiler is faster by ReleaseCandidat on Saturday December 18, 2021 @03:05PM (#29608025)
There is no reason, the fasted C++ code is actually C - I really don't know why somebody wrote that as C++.

https://github.com/hanabi1224/Programming-Language-Benchmark...

https://github.com/hanabi1224/Programming-Language-Benchmark...

Re:Which programming language or compiler is faster by mwattsun on Saturday December 18, 2021 @01:52PM (#29607327)
I was curious too and found this:

https://stackoverflow.com/questions/2513741/c-vs-c-for-perfo...

from which I gather that if you're using an allocator function that zero's out memory then it will be slower of course.