Reputation: 43
I'm wondering and researching in floating point calculations why Java is slower than C. Actually some algorithms doesn't affect too much. C is faster than Java 3 or 4 times. But in floating point calculation there is a huge difference. Can anybody help me?
Upvotes: 2
Views: 1277
Reputation: 143172
C is compiled and ends up as machine language.
Java is compiled to byte code and then interpreted by the Java Virtual Machine.
Interpreted will be slower than assembled code by definition (you can think of interpreted as line by line assembly .. repeatedly e.g., if inside a loop)
Upvotes: -2
Reputation: 3297
Compilers for middle level languages such as C are smart enough to exploit vectorization and other techniques to provide speed for the same piece of functionality as compared to interpreters for high level languages such as Java who need to a one extra layer of converting the platform independent byte code to platform specific assembly.
Having said that Virtual machine comes with its own overheads for maintaining the state of your program for garbage collection and other activities such as Thread scheduling within the JVM.
To be more specific JAVA floating point operations hurt everyone.
Java’s floating-point arithmetic is blighted by five gratuitous mistakes:
Linguistically legislated exact reproducibility is at best mere wishful thinking.
Of two traditional policies for mixed precision evaluation, Java chose the worse.
Infinities and NaNs unleashed without the protection of floating-point traps and flags mandated by IEEE Standards 754/854 belie Java’s claim to robustness.
Every programmer’s prospects for success are diminished by Java’s refusal to grant access to capabilities built into over 95% of today's floating-point hardware.
- Java has rejected even mildly disciplined infix operator overloading, without which extensions to arithmetic with everyday mathematical types like complex numbers, intervals, matrices, geometrical objects and arbitrarily high precision become extremely inconvenient.
Upvotes: 4