Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Lets say my friend needs to write an incredibly math heavy application. He has thought about using C++, but he wants to write it in Java. Just how slow is Java compared to C++? His application will need to solve something like a 10000 by 10000 matix per frame, so performance is important. What should I tell him? Make it in Java or C++? And why?

(He really really wants to use Java, but if the application is going to run at snail speed, he's going to be extremely disappointed.)

share|improve this question

closed as not constructive by maple_shaft Mar 16 '12 at 17:29

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.If this question can be reworded to fit the rules in the help center, please edit the question.

1  
Have a look at shootout.alioth.debian.org/u32/… to know a little on the same :) –  c0da Mar 16 '12 at 16:43
1  
Why doesn't your friend post here and ask? ;) –  FrustratedWithFormsDesigner Mar 16 '12 at 17:20
    
Per frame of what? Is the problem is amenable to parallelization? If so, that might give the best performance boost regardless of language choice. –  Angelo Mar 16 '12 at 17:28
    
Why didn't anyone say "Check your algorithm before you worry about language"? Do some prototype testing based on your problem. –  Paul Mar 16 '12 at 19:47
    
If I were your friend, I'd consider putting this heavy math on your gpu, using opencl or cuda. :) –  Max Mar 20 '12 at 8:08
add comment

6 Answers

This is one of the few cases where the Great Benchmark game actually might have practical value. The initial link is a 64 bit quad core, this one is a single core 64-bit. Take anything like this with a grain of salt though, optimization and language tricks can work wonders for things and not having to deal with direct memory allocation can be life saving.

share|improve this answer
1  
+1 on the grain of salt comment. Java's runtime for example can adjust to inputs on the fly and in some cases run faster than C++, it always depends on a bunch of variables. –  Martijn Verburg Mar 16 '12 at 17:01
    
In C++ you don't have deal with "direct memory allocation" anymore. You have to deal with memory management, but you also have to do it in Java -- only problems are different. –  quant_dev Mar 16 '12 at 17:21
add comment

The advantage C++ has in performance-critical applications: it can use "intrinsic" functions to access the platform-specific vector math functions of the processor.

As far as I know, Java has no such abilities -- however, if you need to solve large matrix systems, you should use a mature native linear algebra library anyway; the call overhead will be minimal in that case. And, if you need to use the GPU to accelerate your math, there are apparently Java bindings for OpenCL (and also for OpenGL, in case your problem is better adapted to conventional rasterization).

The advantage C++ has is for math-intensive tasks with lots of small operations, but which are not suitable for GPU acceleration. Computer graphics actually has a lot of computation like this, but your actual milage will depend on your specific problem.

share|improve this answer
add comment

Java has an unfair reputation for being slow. That may have been the case at one point, but these days it has been optimized to the point where in most cases there is little performance difference between the JVM and natively compiled code. Your friend's ability to write computationally efficient code- which will probably be tied to his familiarity with the language- will outweigh any performance benefit from choosing one over the other.

share|improve this answer
2  
I read many opinions like this. But at the end of the day, every IDE written in Java I've ever used has been slower than Visual Studio. –  quant_dev Mar 16 '12 at 17:26
    
@quant_dev: And recent iterations of Visual Studio are slower (by a pretty obvious margin) than when it was written entirely in C++. –  Jerry Coffin Mar 16 '12 at 18:40
add comment

I would start by programming the numerical core of the application in Java and testing the performance. If it's acceptable, build the Java application around it and forget about C++. If it's not, your friend can either build the whole app in C++ or write the numerical core in C++ and add a Java UI layer, calling the C++ via JNI (or JNA). Mind you, JNI has its own performance overhead and it's a bit tricky to do right, so it may turn out that doing the whole thing in C++ is actually easier.

share|improve this answer
add comment

Make it in Java or C++? And why?

He should pick a language he feels comfortable with, and maybe he knows better.

Math calculations are optimized for both languages anyway, therefore I do not expect c++ application to be faster then java.

share|improve this answer
3  
That's a very optimistic view of Java numerical performance. –  quant_dev Mar 16 '12 at 17:17
    
@quant_dev I expected +50 from java fan boys, but it didn't happen ;) –  BЈовић Mar 16 '12 at 18:30
add comment

C++ is faster, but only by a hair; Java has undergone several optimizations over the years. The Java vs C++ performance wars still rage on, with benchmarks and counter-benchmarks from both sides. What matters though is what you can accomplish with the language and how easily you can accomplish it. I'd go with Java since it's easier to use and has cross platform support.

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.