Jump to content

  • Log In with Google      Sign In   
  • Create Account


C++ vs. Java: an experience


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
9 replies to this topic

#1 HScottH   Members   -  Reputation: 508

Like
4Likes
Like

Posted 27 February 2014 - 01:25 AM

Greetings,

 

I am writing a block-based game. I began in Java using JOGL, having had recent experience with that pair on another project.  Later, in defference to the many conversations about JOGL vs LWJGL, I ported to that library and posted the experienc here (performance was identical, but LWJGL easier to use).

 

Well, in speaking with some friends I learned that it's not as hard to write portable C++ code as I thought, particularly with C++ 11 (I was terrified of having to mess with XCode to get a Mac port working!).  So, given that C++ is my favorite language, I ported my game again.

 

Why I think this matters

There are often threads here (and elsewhere) asking about what language a person should learn, or asking if certain language/platform choices are good ones to make games on.  My view where most OpenGL-based games are concerned is that the performance bottlenecks are almost always GPU based, and as modern GL use uploads all data to the GPU, the language will rarely matter.  Consider Minecraft: could this game be "better" or faster if it had been written in C++?  Sure, the code could run faster (if written well), but the frame rate would not necessarily improve.  To say it another way, Minecraft could be improved through optimization within its current language (Java) more than by a move to another language.

 

So why would I rather work in C++?

I know it best.  I have many more years of experience in C++, and having also many years of assembler, am able to write much more performant code.  For the vast majority of uses, this would not matter, but being a bit of an optimization fanatic, I like to keep resource use low "just in case."  This is impractical, but part of my character as a programmer.

 

So what were my results after porting?

My Java code was written "just to make it work," i.e., no optimization whatsoever.  The C++ code was as trivial a port as could be done, also with no optimization.

 

Note: I spent a fair amount of time optimizing use of the GPU, and that work is language agnostic, but really no work optimizing for the CPU.

 

The program renders as many as four million block faces, meshed into perhaps two million triangles (one million quads). It uses frustum culling, z-test and a few tricks to eliminate geometry that does not need to be rendered, but nothing else.

My results were as follows:
* No change in framerate (~265 fps, minimum, in both languages, and > 3,000 maximum)
* No change in CPU use while playing (~10%, but it is as yet single threaded on a 6-core box)
* Perhaps 15% faster world generation time in C++
* Slightly smaller memory use in C++ as apposed to Java

 

Although the numbers differ, the difference (no difference) is similar across many different PCs and GPUs.

From personal experience, I know that*
* I can bring the CPU use down for C++, and also bring down the world-gen time (with a little optimization)
* I can bring the memory use down dramatically in C++

 

But one more part of the experience bears some mention: development and debugging.

 

In Java, all builds are created equal. They all run with full debug support, and they run fairly fast.

 

In C++, release builds are the fastest but lack even trivial debug support. Debug builds are unbelievably slow.

 

Also, a Java port to another platform requires a few command line switch changes, whereas a port in C++ requires a platform-specific build which, while easier than I had thought (using glfw, etc.), is still much more technically challenging than with Java.

 

Conclusion

For an experienced C++ programmer, and particularly working on a long-term project, in which future requirements may demand the utmost performance, and for which the time to do platform-specific builds is comparitively small, C++ is what I will choose to use.

 

However, if tasked with building anything with more certain requirements, or in which the CPU-related needs are both known and moderate (at least not high), Java is a better language, at least insofar as I know I can complete the work and cross-platform port in far less time.

To put it another way: assuming Notch is reasonably skilled, Minecraft would not be better nor give higher FPS if written in C++.  Therefore, for most games, people should choose the language they are most productive in.

A few details, for those that care
The data in this post does not depend on tech specs, but I know I often want them when I read such things, so here they are.

I am running Windows 8.1 on an i7 980x (6-core) CPU with 24 GB 1,600 tripple channel DDR-3. It has a Radeon HD 5870 GPU.  I am developing with Microsoft Visual Studio 2013 Express (for C++) and Intelli-J Community edition (for Java).



Sponsor:

#2 Ashaman73   Crossbones+   -  Reputation: 7101

Like
5Likes
Like

Posted 27 February 2014 - 02:22 AM


My view where most OpenGL-based games are concerned is that the performance bottlenecks are almost always GPU based, and as modern GL use uploads all data to the GPU, the language will rarely matter.

I think, that this is not true. The GPU is so lightning fast nowadays, whereas the CPU is still lacking behind. GPU performance is even more scaleable (just turn off some features), whereas the CPU load is often more restricted and can't be scaled as well. Sure, push the settings to max and your GPU will be the bottleneck, but playing your game on an old CPU will kill your performance for sure.

 

Minecraft is written in java, yes, but it depends a lot on player content creation. There's no fancy AI, physics, simulation, scripting etc. going, in general not a lot of CPU demanding tasks.

 

C++ is only 'necessary' at a certain 'threshold'. Modern CPUs are capable to run games in java without issues, but eventually high-demand engine will still need this little bit of more performance and control to deliver the best out of it. In this sense, you could say, that most hobby/indie devs might be happy with java, but eventually it depends on your real CPU demand.



#3 tonemgub   Members   -  Reputation: 833

Like
0Likes
Like

Posted 27 February 2014 - 02:33 AM

 

The video is a bit old now, but still...


Edited by tonemgub, 27 February 2014 - 02:34 AM.


#4 HScottH   Members   -  Reputation: 508

Like
0Likes
Like

Posted 27 February 2014 - 01:33 PM

... but eventually it depends on your real CPU demand.

 

 

Exactly.



#5 Kaptein   Prime Members   -  Reputation: 2039

Like
0Likes
Like

Posted 27 February 2014 - 06:29 PM

The GPU may be strong, but with voxel worlds you suddenly find yourself bottlenecked just about everywhere. :(

The good thing about the CPU-side is that you can use threadpools to slowly generate the world meshes, which is basically a must-have for complex voxel engines with raycasted lighting etc.

But the GPU side stays the same, and is not quite solved for me. Decent FPS when having a monster GPU is easy enough.

I only wish i had more options when it came to how i rendered the data, especially with how many commands i need to go through. The rest is scaleable - such as settings and especially view distance. In the end you could run my engine on just about anything with opengl 3.x support, more or less. It just wouldn't be the same with terrain all the way into the horizon.



#6 DvDmanDT   Members   -  Reputation: 847

Like
0Likes
Like

Posted 27 February 2014 - 08:15 PM

Whenever I play Minecraft I keep thinking to myself "I wonder how much faster/cleaner/more stable this would have been if they had something like value types in C#/.NET". I'm not saying Java is a poor choice for game development, but rather that each language has its strengths and weaknesses, and that I'm skeptical MC is a game for which Java truly shines.

 

Also, doing a straight port from one language/platform to another is rarely intresting to look at since it usually means you won't take advantage of the new language fully. It's very possible that you could have created very different solutions in C++ that simply are not possible in Java for example. Of course, if you're GPU bound then it won't matter.



#7 tonemgub   Members   -  Reputation: 833

Like
0Likes
Like

Posted 03 March 2014 - 11:04 AM


Also, doing a straight port from one language/platform to another is rarely intresting to look at since it usually means you won't take advantage of the new language fully.

I would only apply this to his memory-use comparison. Java has garbage collection, so it probably keeps objects around in memory for longer than his C++ implementation does. The rest of the comparsion results seem interesting to me, especially the point he makes that optimized C++ code doesn't yield better frame-rates than the Java counterpart. To me, this clearly means that game development nowadays is all about GPU, rather than CPU optimization.

Ok, maybe the point about CPU use is also invalid, since  a single thread running full speed on a six-core CPU would be expected to consume 16%. He mentions ~10%, but if that's 10% of one core, or 10% of all cores is relevant too. And it also means that his tread does some waiting somewhere that might not be needed in such a comparison (since it never gets to 16%, or is that power-saving?).


Edited by tonemgub, 03 March 2014 - 11:21 AM.


#8 DvDmanDT   Members   -  Reputation: 847

Like
1Likes
Like

Posted 03 March 2014 - 04:23 PM

No, it would be expected to consume ~8% of total due to Hyper Threading on that cpu (12 logical cpus). This means we cannot be sure that he is in fact gpu bound, he could just as well be cpu bound. Also, even if he is gpu bound, that only means that _he_ is gpu bound, not that games in general is. It's also possible that he shifted work to the gpu from the cpu because some rendering/optimization techniques are hard to do effectively with Java that would have been easier in another language such as C++. Please note that I'm not trying to compare C++ to Java with this statement but rather point out a flaw with this type of language comparison.



#9 HScottH   Members   -  Reputation: 508

Like
0Likes
Like

Posted 19 March 2014 - 11:00 PM

UPDATE

 

Regarding some of the posts here: my game was GPU bound.  I have deep and lengthy experience with JNI and other native issues, and I promise that ~8k JNI calls per frame were not taxing the CPU, the RAM or the PCE-e bus, although if the JNI calls were powerfully contributing to my FPS one would have expected an improvement in C++ (no marshalling), which did not occur.  (I have since analyzed performance on several GPUs and found the bottlenecks to be geometry processing on all but one, and fill rate on that outlier, neither of which are language related).

 

Now, the comparison I made is salient for the type of game I am building: largely static geometry, whereby nearly everything is uploaded to the GPU for continual processing.

 

Java garbage collection can seriously impact games, albeit moreso on core-limited devices (such as mobile), but my code had no measurable loss of performance from garbage collection as I am a bit of an optimization geek and everything that could cause these problems is pooled (I have used and taught Java since about '98).

 

I have recently done some testing on various GPUs and found some drivers do rather unpredictable things with respect to how they move data from RAM to VRAM.  In particular, standard "best practice" methods for moving data do not apply on some of the Radeon 5xxx series drivers, in which glBufferData is every bit as fast as mapping and uploading in a background thread.  My tests show that glBufferData initiates a DMA transfer from the RAM buffer directly to the GPU, whereas conventional knowledge suggests that the driver first copies the data.  Of course this is an optimization, but is means if you reuse the buffer before the DMA transfer is complete, you will get some very interesting results (and I did!).  Ironically, this particular optimization will benefit Java implementations a little more than C++ ones.

 

Finally, my real update: after re-writing some code--particularly my task scheduler (logic to maximize performance on all CPU cores)--my performance is up about 10x from what I started with.  When I back-ported this logic to Java, I got a 3x improvement, so something about Java threads causes them to suffer a significant loss compared to native C++ 11 threads.  This is visible watching the task manager: whereby both programs have the same number of threads, only the C++ 11 peggs all cores, and the Java one leaves them at around 35% utilization.

 

I would love to pursue this and underand why for my own edification, but as I am no longer working in Java, I just don't care any more :-)

 

In conclusion: I believe the following statement to be true: if you write a game in C++ and it uses on average less than 50% of the typical CPU, the game could be written to perform nearly as well in Java.  If it uses less than 30% of the CPU, it could perform as well in Java.

 

This is to say what I have always said: you should program in the language you know best (C++, Java, C#, VB,...) as that will maximize your chance of completing a game.  If you have performance problems, chances are you can fix them algorithmically, and if they are FPS related, chances are you can NOT fix them with a language change (unless you are CPU bound).



#10 TheChubu   Crossbones+   -  Reputation: 4063

Like
1Likes
Like

Posted 20 March 2014 - 12:19 AM


I am a bit of an optimization geek and everything that could cause these problems is pooled (I have used and taught Java since about '98).
What resources do you pool often? 3D applications often use lots of vectors and matrices. Java has the peculiarity of needing direct buffers for interfacing with native code like OpenGL lib. How do you handle those?

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

 

My journals: dustArtemis ECS framework and Making a Terrain Generator





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS