C++ sucks

Peter Korsten EMAIL HIDDEN
Thu Nov 22 21:52:13 CET 2007


Martin Naef schreef:

> I'm not denying that. But when I try to do low-latency machine vision 
> processing with a camera running at 200Hz, the last thing I need is 
> garbage collection kicking in at the wrong moment, and bounds checking 
> is going to kill the performance. Of course, your environment is 
> different - if your application crashes the server, all the speed in the 
> world doesn't matter anymore, hence a tool with lots of safety devices 
> is what you need.

One thing you have to keep in mind is the progress that dynamic 
optimisation has made over the last few years. There's no comparison 
between the 1.0 and 1.6 versions of the Java virtual machine. And I 
expect that Microsoft's CLR offers similar optimisations.

For instance, if you see this code:

int[] a = new int[10];
for( int i = 0; i < 10; i++ )
	a[i] = i * i;

there won't be any lower bounds checking (and possibly no upper bounds 
checking either), because the compiler figures out that 'i' will never 
decrease.

But garbage collection and dynamic optimisation is indeed the reason why 
Java is not very well suited for a real-time system, because it makes 
performance unpredictable. Raw speed, though, is not nearly as much of 
an issue as it used to be. And as dynamic optimisation is improving, 
it's reasonable to assume that at some point it will overtake static 
optimisation, if that hasn't happened already.

> And if you believe that garbage collection ensures that you don't leak 
> memory, think again.

Oh, I don't think that. But it takes away a lot of donkey work from the 
programmer, be it at the expense of some flexibility.

> Ok, fair enough - you found something that doesn't work. I'm sure I 
> could easily construct something that won't work in Java. What's the point?

That it's ugly. :) By the way, what you could construct in Java that 
won't work the way you expect it to, is when you start using generics - 
Java's version of templates. :)

> As above: When they designed Java, they already had all the feedback 
> from C++ programmers. The "horrible Syntax" is a matter of taste and 
> personal preference.

Hmm:

class B : public A
{
	virtual C& c() = 0;
};

public interface B extends A
{
	C c();
}

> There's a reason why I chose the Ferrari: It's fast, if you know what 
> you're doing. For everyday driving, speed doesn't matter. But for what I 
> do, it's crucial, and C++ gives me the right balance between a "proper" 
> language while not preventing me from digging really deep.

Java is not slow. It used to be, but unless you're doing timing-critical 
tasks, or very specialised ones like BER decoding where the absence of 
certain features means you have to jump through hoops, it's fast enough 
to go through many megabytes of data without breaking a sweat.

> That means C++ is a great language. For me.

No doubt, but if you're doing any processing that is not real-time, or 
anything that vaguely resembles accessing a database or doing network 
communication, you should have a look at Java. The implementation of 
object-orientated programming and the standard libraries are miles ahead 
of C++: interfaces, 'finally' blocks, proper exception handling, etc.

Not to mention reflection, dynamic class loading, multi-threading 
without the pain of POSIX, remote method invocation, and platform 
independence (write it on Windows, run it on a mainframe).

- Peter

- Peter



More information about the music-bar mailing list