Advertisement

Java is fast?

Started by April 16, 2006 06:09 PM
110 comments, last by rip-off 18 years, 5 months ago
Anybody can write "a program" that runs faster in Java. Give them enough knowledge and they can probably write a program that runs faster in Python than in C++! For example, overuse of "new" in C++ is bad, but it is optimized in Java and other languages that require constant use of it.

The question isn't whether or not you can run some piece of test code faster in Java, it's whether or not you can make some real-world app in both highly optimized Java and highly optimized C++ and benchmark the two together. Of course, the only people who would waste their time doing that are the Java-heads, and they wouldn't do it because it would prove that C++ is faster.

There is no question: C++ has NO overhead. The JIT compiler and runtime environment of Java is overhead.

Java users argue that the JIT compiler allows the program to optimize for the particular machine when it compiles. However, this optimizing is again more overhead. In C++ you can statically optimize for a particular machine at compile time (run different code paths on your assembly code for different processors) which has NO overhead.

Anybody can build a statistic. That doesn't make it true.


Note: Speed is not everything. My most recent game project was written in Java (for the Processing environment), both for web support and ease of development. It's just NOT faster.

Check out my new game Smash and Dash at:

http://www.smashanddashgame.com/

Quote: Original post by Anonymous Poster
But both the performance and code associated with java are lackluster ;)

*No const modifier.
*No operator overloading (except for the String class, which in itself should convice you that the language is really badly designed).
*Really bad support for generic programming (cant use it with primitives, generic classes storing different object types are actually same type etc).
*No support for call by reference on primitive types, so you need to wrap everything in objects.
*Lots of calls to new (like initializing an array, where you need to loop and use new afaik).
*Only very recent support for enumerations and not very flexible compared to c++ afaik ( I like to use enumerations when indexing arrays, makes everything much nicer to read and easier to comprehend).

I could go on...



* final acts same as const when applied to fields.
* operator overloading was left out on purpose to make reading code easier. True this means more code is required but it is also true java code is easier to read.
For example if had class called Person. What would new Person() += new Person() be doing?

* generics were added to Java 5
* autoboxing and unboxing was added to Java 5
* enums in Java are just classes meaning they can contain methods


- Chris
"More computing sins are committed in the name of efficiency than for any other single reason - including blind stupidity" - William Wulf

"Any fool can write code that a computer can understand. Good programmers write code that a human can understand"

Advertisement
Quote: Original post by Konfusius
To be 'fair', you should use the server Java VM, because it is reportedly faster than the client version.

You're right, it's sigificantly faster. I got 34 seconds for 10 runs. Not bad at all.

For the C++ version I enabled some relevant optimizations and made some trivial code changes (without changing the actual behaviour of course). I got 28 seconds without effort.

Going back to Java I tried the same changes there (basically inlining everything). I got 29 seconds. Nothing short of impressive. Still not beating the C++ version by 7x though.

I think this actually tells the whole story. Java can't beat well optimized C++ code. It does have an impressive compiler (actually the server runtime) though, that turns shitty code into something that gets fairly close to the optimal. With bigger projects I wouldn't expect it to perform this well though. This particular test just happened to generate fast code. I am absolutely sure there is no test where C++ is slower after optimization, and I'm also sure there are tests where Java runs at a fraction of the C++ speed without any optimization being able to help it (unless rewriting the compiler/runtime).

The bottom line is that if you want optimal performance there's nothing better than C++ (where you can access all low-level constructs and even assembly). Java is nice for tools but it's not the language to write Doom 4 with.
Quote:
* operator overloading was left out on purpose to make reading code easier. True this means more code is required but it is also true java code is easier to read.
For example if had class called Person. What would new Person() += new Person() be doing?


I don't want to get into the Java VS C++ "Which is better" argument, because neither is "better" (though my above post made it obvious what I think as far as speed goes).

However, only an idiot C++ programmer would make Person() += new Person(). This is dumb code, and I'd slap the programmer.

However:

Point2D p1(1.0f, 0.0f);
Point2D p2(0.0f, 1.0f);
Vector2D v1 = p2 - p1;

Is much more intuitive to read than:

Point2D p1(1.0f, 0.0f);
Point2D p2(0.0f, 1.0f);
Vector2D v1 = p2.minus(p1);

Not a huge difference, but it's a simple example. Operator overloading can make code much easier to read if you don't have a moron behind the wheel. Of course, a bad programmer can make a simple function call seem obscure...

Check out my new game Smash and Dash at:

http://www.smashanddashgame.com/

Quote: Original post by Anonymous Poster
But both the performance and code associated with java are lackluster ;)

*No const modifier.


Agreed

Quote:
*No operator overloading (except for the String class, which in itself should convice you that the language is really badly designed).


Strings always get special treatment. In c++ they just get less. You are allowed to do things like char str[] = "..."; which isnt entirely consitant either.

I have to say the only operator overload I make use of frequenlty is operator[] and operator= and operator==. I would like if the comparison and indexing operators were available in Java, but alas...

Quote:
*Really bad support for generic programming (cant use it with primitives, generic classes storing different object types are actually same type etc).


I believe java 1.5 goes some way to improving this.

Quote:
*No support for call by reference on primitive types, so you need to wrap everything in objects.


Agreed, although I have never really needed to do this in java (Disclaimer: I use java at college, I use c++ myself, so yes I am aware as to how it can be useful, and no the variety of programs I write in Java isn't much )

Quote:
*Lots of calls to new (like initializing an array, where you need to loop and use new afaik).


Agreed. But I have heard that in GC langugeas calls to new are virtuall free, so its just a syntactical thing.

Quote:
*Only very recent support for enumerations and not very flexible compared to c++ afaik ( I like to use enumerations when indexing arrays, makes everything much nicer to read and easier to comprehend).


I haven't used enums in Java, no comment ( although I'm suprised that you cannot use them for array indexing ).

Quote:
I could go on...


I will for you :)

*No Destructors!

This makes me sad. I have heard of finalisers and have also heard that they are not guarenteed to be called for some reason. Please, someone prove me wrong about this!


But enough of that, I'm in 2 minds over Java. I thinks its quite nice in some ways, and that many of the points most frequently made against it are not important enough to be considered in a language vs language discussion. I mean really most of them boil down into "why isn't Java c++" anyway.

But in general, most of the games I make could be made equally in Java or c++, and the end user probably wouldn't notice the difference. It could be easier on me ( in terms of dev time ) but I woun't make the switch.

Almost 2 years of c++ and I'm already stuck im my ways [grin].

Someday I will look at what c# can offer me, but not soon. Maybe when it has better cross platform support.
Quote: Original post by GentlemanHal
"You don't have to search through too many blogs or Slashdot postings to find confidently worded statements like "Garbage collection will never be as efficient as direct memory management." And, in a way, those statements are right -- dynamic memory management is not as fast -- it's often considerably faster."

http://www-128.ibm.com/developerworks/java/library/j-jtp09275.html

I'm sure that with a simple custom allocation/deallocation implementation C++ is faster. In many cases you can even replace new/delete with a stack based approach, which makes it as simple as i++/i--. Java is also incredibly memory hungry in comparison.

Also, Modern C++ Design has a whole chapter about Small-Object Allocation.
Advertisement
Quote: Original post by Lazy Foo
Quote: Original post by jfclavette
Says who ? Works pretty damn well for me.


Says the industry. You can switch tools whenever you want, but companies can't afford to gamble.

DirectX, Java, you-name-it wheren't adopted overnight. A technology has to prove itself first.


Yes, because right now they are using the same cross-platform language, C/C++, to develop games for Linux, Solaris, MacOS, and Windows. </sarcasm> [grin]

Whether mono is considered "mature" is completely irrelevant to whether c# will come to dominate game dvelopment. They don't develop for those platforms anyway.

It should also be noted that mono's main "problem" is the fact that Microsoft's own C# implementation isn't standard compliant. Or, rather, the standard doesn't include things like Windows.Forms, etc, so a lot of the .NET framework goes above and beyond the standard.




Quote: Original post by C0D1F1ED
JIT-code will never win from native code. The run-time compilation takes extra time and optimization isn't a fast operation. C++ can spend minutes on optimization without a problem, while Java only has fractions of seconds to perform processor specific optimization. Else the user will notice the slowdown too much. The end result is that the C++ code will be more optimized, without any run-time overhead.


Once it's compiled, though, it's compiled. So, as the programs run to infinity the performance difference (all other things being equal after compilation) converges to zero. So, as someone else noted, it all comes down to the compiler. With a better compiler, JIT Java could definately outperform C++.

It just depends on the situation. No blanket statement can be correct.



Quote: Original post by C0D1F1ED
I'm sure that with a simple custom allocation/deallocation implementation C++ is faster. In many cases you can even replace new/delete with a stack based approach, which makes it as simple as i++/i--. Java is also incredibly memory hungry in comparison.


But, by going the stack based way you either:

a) become a memory hog just like you claim Java to be!

(Disclaimer: I have no opinion of Javas memory consumption )

b) End up having yo rearrange the stack when objects in the middle are deleted, thus taking up CPU time.


Either way its not a magic bullet.


There is, I believe, another important consideration when looking at the future of languages such as C/C++. If I am a developer writting a library for physics/graphics/numerical processing/whatever I want my library to be used by as many people as possible. This is where I get my money. I might choose to write it in Java, and if I use the various clever techniques which Java provides I am sure I can get it very fast. Who will use it? Java programmers. I might instead choose to write it in C#. Who will use it then? .NET programmers.

If I create in in C/C++, however, then it can be used by everybody via easy to create language bindings. As a developer I maximise the number of people who will make use of my product and therefore maximise my profit.

I'm sure it is possible to get Java to call code written in .NET. Or to get VB .NET to call the Python libraries. But I don't believe it's as easy. And what's more, would anyone use it? We all know these language wars are pointless but the fact remains that people get very attatched to the language of thier choice. What self-respecting Java programmer would hook into the .NET libraries? Or vice-vesa? Most, however, will make use of libraries written in C/C++ and compiled to machine code.

From this perspective C/C++ give the greatest flexibility.

This topic is closed to new replies.

Advertisement