views:

480

answers:

8

Speed is an important part of choosing a programming language.

Apparently, C++ is(for most people) the undoubted ruler when it comes to speed. Yet when asked with evidence to back this up, nothing can be offered.

Usual excuses include:

  • It's faster than Java
  • There are so many blogs saying it, it must be true.
  • It's the language I've learned and always worked with.

I've tried looking up some benchmarks, but the documentation is either hard to figure out, or the data used to make the benchmark is seriously out of date, sometimes even using Java 3. To then claim that Java is slow, compared to more recent releases of other languages, is an unfair comparison.

It's probably also historical, with C++ having ruled the programming world in terms of speed a long time ago.

But what about today. Can anyone offer very recent benchmarks, with the latest version on the latest systems?

I mentioned Java, but I'm not specifically trying to give Java a better image or anything. It's just that from my experience, Java is by far the language people like to make fun of, for some reason.

+7  A: 

Link from the "Related" sidebar:

What programming language implementation benchmarks are there?

From the top answer:

http://shootout.alioth.debian.org/

Last updated: 2009-12-06

C and C++ have had quite a lot of work put into their compilers. In some ways, they make the programmer do more of the work, but in return they provide the fastest possible code. Thus they remain the best choice for top performance.

The best chance to beat C/C++ is with a highly parallelizable problem and a functional language like Erlang. If your Erlang program automatically scales itself across 16 CPU cores, it might finish sooner than a C++ program. But in theory you could hand-code the C++ to scale and beat the Erlang.

In theory you could hand-code some assembly language and beat even a C compiler. However, on modern CPU architectures this has become very difficult; the compilers never get bored and don't make many mistakes, and they can really wring the best performance out of a CPU.

steveha
I'm getting a bunch of "unexpected T_STRING" errors. Not a single one is showing.
WebDevHobo
Be careful to select the "single" core page there. Some languages that implicitely use threading skew the results otherwise, in a way that is not representative for most normal programs.
Marco van de Voort
@Marco van de Voort - Be careful to select the "quad" core page there if you want to see programs that use the multiple cores that are available on most every new computer. Notice that for some languages, programs that use multiple cores have not been contributed yet.
igouy
@WebDevHobo The website error has been fixed.
igouy
@steveha: As long as compilers have been available, they have been marketed as "creating as fast and compact code as Assembler". These claims have always been proven wrong. It is difficult to beat a compiler only if you do not know how to program in Assembly. And that is exactly the problem: there are very few programmers these days who can program in Assembly language.
PauliL
@PauliL, please link to an example that demonstrates your claims. Also, please consider crazy architectures like the Itanium and certain DSP chips, where the optimal code is not at all easy to figure out. I did concede that, in theory, you could hand-code assembly and beat a C compiler. If the reality was as open-and-shut as you are claiming, then why is assembly-language so little used these days?
steveha
+3  A: 

First of all, we start with the self-evident observation that code running in an emulated VM will never be faster than the same code running on the metal.

Then, we note that a JITter does not have as much time to optimize code as a native compiler does, and conclude that unless the JITter can take advantage of CPU features that the native compiler cannot, it is not going to out-perform code targeted for the specific hardware and fully optimized at compile-time.

Anon.
Usage of an interpreter, JIT-compiler, NGEN-compiler or static a compiler is orthogonal to the choice of language.
Franci Penov
It can and does take advantage of CPU features, as well as information about the code itself. For example a JIT compiler can do run-time dead code elimination, as well as method inlining. Both of these are more powerful than the static compile-time equivalent.
Steven Schlansker
@Steven - you are right that what codegen/exec tool you use has perf implications. However, that is still orthogonal to the choice of language in the general case. Of course, the toolsets for some languages are limited to an interpreter or a static compiler only, so people say "language perf" when they really mean the perf of the tool. However, the toolsets for other languages span the whole range of code-gen/exec possibilities. "Language perf" does not make much sense for such languages. Example: is C++ faster than C#, which can be precompiled, NGENed, JITed or interpreted?
Franci Penov
Sure. There's a reason I was replying in a comment to Anon's answer, rather than as a top level answer for the actual question asked :)
Steven Schlansker
+8  A: 

"Speed is an important part of choosing a programming language"

IF speed is an important part of the problem you are trying to solve.

In a lot of cases it isn't.

Firstly, a lot of slow software is the result of badly thought out algorithms rather than the implementation language.

Secondly, memory & processor speed has reached a point now that you can write the concepts in a "slow" language and then profile to find the areas that really need speeding up.

Matt Breckon
Thanks, exactly what I wanted to write. Most of the time "speed" is the result of good implementation (algorithm and data structure choices), and not of the raw execution speed. Also, the less you have to do yourself, the less you run into the problem of re-inventing the wheel (which usually isn't even round, e.g. is buggy).
Lucero
This is totally irrelevant. Clearly when picking a language nobody's going to compare efficient implementation in one with inefficient in the other.
Michael Krelin - hacker
@hacker: It's totally relevant. Most code written is so slow that it doesn't matter if one language is a few percent faster than another.
Guffa
Guffa, it *does* matter if the code is written efficiently. And what happens when it's not really doesn't matter.
Michael Krelin - hacker
@hacker: That's exactly what I'm saying. The speed of the language only matters if all the code is optimised for speed, and most code isn't. So, you contradict yourself.
Guffa
One could as well say "In a lot of cases one doesn't know up front"
Marco van de Voort
Guffa, and where's the contradiction?
Michael Krelin - hacker
+1  A: 

I would say that what people mean by "It's faster than Java" is that C++ compiles to native code, not to bytecodes such as Java or .NET.

Konamiman
which is not necessarily a performance drawback, since .NET (and some Java runtimes) use just-in-time compilation.
Heinzi
JIT still has a perf hit when running the app, but C# also can be pre-compiled to native code, or NGEN-ed at deployment.
Franci Penov
That being said: are there other languages which also compile to native code? Or in development? There is Google's Go, but that's fairly recent, and C/C++ is getting kind of old. Something new should be done. Perhaps Go is the answer, perhaps not.
WebDevHobo
JIT:ed code has the potential to generate code that is even faster than native code. As it's compiled on the computer where it runs, it can optimise the code using the exact characteristics of that processor, instead of making general optimisations that has to work for a whole range of processors.
Guffa
@WebDevHobo Yes there are other languages that compile to native code - some of which use C as intermediate code and then use a C compiler - see http://shootout.alioth.debian.org/u64/which-languages-are-fastest.php
igouy
A: 

I have researched tis topic as well. You need to look video game development companies. Almost all commercial video games today use c++/ASM to squeeze every bit of performance out of the software. Many 3D rendering engines are written in c++ and not Java. Can you imagine if 3d engines were written in java? I will tell -- it would be very coppy and unplayable.

Luke101
A: 

C++ gives the programmer access to the low level details of the underlying machine (historically, this is due to C++ being largely an extension to C, which itself was designed for coding low level system software).

For example, all data addresses are explicitly visible to the C++ programmer. Knowing certain parameters of the underlying machine (cache line size, cache index function, types of available prefetchers, etc.) the programmer can make sure that the data:

  • is aligned (hardware instructions are usually faster on aligned data)
  • makes full use of the cache (as opposed to thrashing which is very ineffienct)
  • is prefetchable (which usually means it's laid out in continuous chunks).

Another example is array bounds checking. An array indexing statement A[i] = 7 is translated by the C++ compiler into an scaled addition of A and i and a store of 7 to the resulting address. When the compiled program is run and i happens to be greater (or equal) than the size of the array pointed to by A, the store will still execute and probably corrupt some data structure. In a more managed language, such as Java, all array accesses are checked and a bounds violation causes an exception. However, C++ does not have that overhead and thus is faster.

Other examples include explicit memory management (no garbage collection, which is a big source of overhead) and ability to examine sizes of the primitive hardware datatypes (see climits.h)

Also, C++ is often used with non-standard extensions, such as intrinsic functions. Intrinsic functions are functions that translate to specific machine instructions (obviously an architecture-dependent feature). For example, you could use _mm_mul_ps() which would translate to the x86 SSE MULPS instruction that calculates four floating point products at once.

The overall moral is if you know exactly what you want to do with the underlying machine to get performance, C++ will let you do it, while higher level languages such as Java may not. Of course, this performance advantage comes at a significant cost: the absence of many safety nets that can help you catch your mistakes.

eleventy
-1 since one are SSE intrinsics part of the C++ standard?
Marco van de Voort
Moreover, I think the main design criterium of C was compiling a large as possible program that would fiting into a shitty PDP7's memory. They weren't exactly free to implement stuff that required more cpu/memory, even if it would have helped performance
Marco van de Voort
I explicitly stated that SSE intrinsics are non-standard. The point is that many non-standard architecture-dependent extensions are available and are often used in high-performance C++ programs. Other languages usually lack such extensions.
eleventy
C was actually first developed for PDP-11 (C's predecessor, B, was used on PDP-7). Regardless, the purpose of both languages was writing system software (operating system, linker, etc.) See "The development of the C language" by Dennis Ritchie in the second ACM SIGPLAN conference on History of programming languages, 1993.
eleventy
A: 

C++ is definitely not a ruler when it comes to speed. C is superior to C++ and Assembly is superior to C. Generally, the lower level language, the better performance (if the programmer knows how to use it).

Object oriented programming is another thing that causes huge overhead. Both C++ and Java, of course, have this problem. However, C++ compilers can be used to compile plain C code, which allows better performance.

Typically, when an application is "upgraded" from C to C++, it becomes 10 times bigger and 10 times slower. Micrografx Designer was a typical example of that.

It is unfortunate that these days performance is so underrated. As a result, applications constantly become slower and slower, forcing the user to constantly update hardware in order to keep the same level of performance and usability.

PauliL
In the hands of an expert, C++ is no slower than C. You can write the same code in C++ as you would have written in C and trivially have the same speed. You can use the features of C++ that are helpful and not expensive, such as templates that expand into very fast code. On the other hand, you can trivially do things like copying an entire string onto the stack when calling a function, and these would hurt performance greatly. Assembly is theoretically fastest, but in practice, on modern computers with modern compilers, it is very difficult to beat C/C++ with assembly.
steveha
Object oriented programming, when done correctly, need not add *any* overhead. It is a way to organize your program. In my day job I write wicked fast C code, and I use object oriented data structures to organize things. C++ compilers are not likely to make better output for plain C code than a plain C compiler, and if the output is better, it is just because the compiler is better and not because it is a C++ compiler. I do not accept your "10 times bigger and 10 times slower" rule; I can trivially write a C program, port it to C++, and demonstrate that it didn't get bigger or slower.
steveha