phu004
|
 |
«
Posted
2013-08-16 00:04:41 » |
|
I have done a little benchmrak comparison between JAVA and C++, with same piece of code, it turns out that JAVA always beats c++ for by small mount. Does it conclude that Java is now faster than C++  ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| #include <iostream> #include <ctime>
int main(){ clock_t begin = clock();
double *numbers; numbers = new double[32000000]; double sum = 0; for(int i = 0; i < 32000000;i++){ numbers[i] = 3141592654.987654321/i; sum+= i/3.141592654987654321/(i+3.14159269876)/(3.14159269876 -i)/(35343.34534 -i)/(3343443242.33 -i)/numbers[i]; }
clock_t end = clock(); double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC; std::cout << sum << "\n" ; std::cout << elapsed_secs;
return 0; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| public class test{
public static void main(String[] args){ long time = System.currentTimeMillis(); double[] numbers = new double[32000000]; double sum = 0; for(int i = 0; i < 32000000;i++){ numbers[i] = 3141592654.987654321/i; sum+= i/3.141592654987654321/(i+3.14159269876)/(3.14159269876 -i)/(35343.34534-i)/(3343443242.33 -i)/numbers[i]; } System.out.println(sum); System.out.println(System.currentTimeMillis() - time); } }
|
|
|
|
|
pitbuller
|
 |
«
Reply #1 - Posted
2013-08-16 00:16:55 » |
|
Java have faster memory allocator than default c++ one. How things change if you just measure loop time? What was optimization level with c++?
|
|
|
|
Riven
|
 |
«
Reply #2 - Posted
2013-08-16 01:05:47 » |
|
It depends on a ton of things, like C compiler flags and JVM runtime flags, and ofcourse, actual algorithms.
Java wins a few benchmarks, C wins most. (that's as detailed as I'm gonna go)
|
Hi, appreciate more people! Σ ♥ = ¾ Learn how to award medals... and work your way up the social rankings!
|
|
|
Games published by our own members! Check 'em out!
|
|
phu004
|
 |
«
Reply #3 - Posted
2013-08-16 01:30:58 » |
|
How things change if you just measure loop time? What was optimization level with c++? If I exclude the array allocation part in the benchmark, Java wins even more  (before it was 1.623s s vs 1.514s, now it becomes 1.624s vs 1.414s, it looks like dynamic array allocation in c++ takes virtually no time) The java version is running under server VM. The c++ version is compiled with g++ and without any flags. I am using windows7 64bits by the way.
|
|
|
|
Riven
|
 |
«
Reply #4 - Posted
2013-08-16 01:39:26 » |
|
... without any flags. I am using windows7 64bits by the way.
at least use -O2 or -O3, and enable MMX/SSE
|
Hi, appreciate more people! Σ ♥ = ¾ Learn how to award medals... and work your way up the social rankings!
|
|
|
Sindisil
Junior Devvie   Medals: 4Exp: 15 years
TANSTAAFL
|
 |
«
Reply #5 - Posted
2013-08-16 01:44:30 » |
|
The array in your C++ code is not dynamic - it's on the stack - "allocation" is essentially just a register add or sub.
If you're not passing any flags to GCC it isn't optimizing at all. Release code is typically compiled with -O2, often -O3. Beating unoptimized C++ isn't really that impressive.
|
|
|
|
Riven
|
 |
«
Reply #6 - Posted
2013-08-16 02:07:50 » |
|
The array in your C++ code is not dynamic - it's on the stack - "allocation" is essentially just a register add or sub.
Maybe it's me showing how n00bish I am, but my understanding was: 1 2
| double arr[123]; arr = new double[123]; |
I'd say he's doing heap allocation and thus 'dynamic', given that he uses 'new'. Having said that, an aggressive compiler would remove the entire array, as each element is written into, then directly read from, never to be used again, so it could just as well be a register read/write.
|
Hi, appreciate more people! Σ ♥ = ¾ Learn how to award medals... and work your way up the social rankings!
|
|
|
phu004
|
 |
«
Reply #7 - Posted
2013-08-16 02:30:40 » |
|
I have tried to compile my program with -O2 -mmmx -msse -msse2, but it makes no difference in performance.
Maybe it's just the code I wrote is very hard for g++ to exploit.
|
|
|
|
Sindisil
Junior Devvie   Medals: 4Exp: 15 years
TANSTAAFL
|
 |
«
Reply #8 - Posted
2013-08-16 02:39:53 » |
|
Nope - you're correct, of course.
I was half listening to someone talking here, and scanned the code very quickly. Seeing a local array of small, static dimension, I must have skipped past the "new". It would never have occurred to me to use "new" in such a case.
I need to learn to pay full attention when posting!
|
|
|
|
ReBirth
|
 |
«
Reply #9 - Posted
2013-08-16 03:00:02 » |
|
It's not "now". I remember read an article on early 2000 that showed Java is faster, but ofc I then found out it really depends on the test case/stress.
|
|
|
|
Games published by our own members! Check 'em out!
|
|
SHC
|
 |
«
Reply #10 - Posted
2013-08-16 03:04:18 » |
|
I found this link. Java vs. C++It's too old, I'm searching for a new one.
|
|
|
|
|
gimbal
|
 |
«
Reply #12 - Posted
2013-08-16 07:36:09 » |
|
Does it conclude that Java is now faster than C++  ? Funny how you say NOW when you use a version of Java that is already balding :s
|
|
|
|
Roquen
|
 |
«
Reply #13 - Posted
2013-08-16 07:38:40 » |
|
Gad's not this again. 1) Writing benchmarks that mean anything is a skill and quite difficult to get right when you know what you're doing. 2) Never draw generalized conclusion from a well written benchmark. 3) Ignore poorly written benchmarks.
|
|
|
|
concerto49
|
 |
«
Reply #14 - Posted
2013-08-16 08:06:42 » |
|
Java wins mostly because you don't have to do any work -- what most people do.
In real commercial applications C++ might be faster because a lot of effort is put to hand tuning most of the code.
This is just like manual vs auto driving.
|
High performance, fast network, affordable price VPS - Cloud ShardsAvailable in Texas, New York & Los Angeles Need a VPS Upgrade?
|
|
|
davidc
|
 |
«
Reply #15 - Posted
2013-08-16 08:36:52 » |
|
I can't comment on the C code, but with java I wouldn't be using System.currentTimeMillis for benchmarking. The resolution of this timer on some systems is notoriously low, with Windows being somewhere around 16ms. When you're looking at result around 1 - 2 seconds, this is significant.
|
|
|
|
relminator
|
 |
«
Reply #16 - Posted
2013-08-16 10:39:55 » |
|
To the OP:
Your forgot a delete[].
|
|
|
|
Roquen
|
 |
«
Reply #17 - Posted
2013-08-16 11:28:52 » |
|
Sigh: numbers should be transformed away. It dead. See points #1 & #3 above.
|
|
|
|
pitbuller
|
 |
«
Reply #18 - Posted
2013-08-16 15:19:35 » |
|
Try to remove pointless array. Does this change stats?
|
|
|
|
Several Kilo-Bytes
|
 |
«
Reply #19 - Posted
2013-08-16 20:25:15 » |
|
Your code could easily be optimized by a compiler to: 1 2 3 4 5 6 7 8 9 10
| double sum = 0; int i = 32000000 - 1; do { sum+= i/3.141592654987654321/(i+3.14159269876)/(3.14159269876 -i)/(35343.34534-i)/(3343443242.33 -i) / (3141592654.987654321/i); } while(i-- != 0); } |
Which would save time because there is no need to allocate the array, write to memory inside the loop, and because comparing registers to zero after doing an arithmetic operation is free. You should write the contents of numbers to a file. (This does not need to be timed.) It would make optimizing it away impossible. You could also compare the binary values of the double precision numbers. The C++ code may be doing something different and thus produce slightly different numbers. Edit: Ignore the reverse order optimization I mentioned. It is something Java would probably do that C++ wouldn't if your numbers were integral types, but Java is not allowed to do so for floating point numbers because it would produce different end results. Would anyone else be surprised if Java was slower than C++ for code fragments that were just plain number crunching? It's almost as if there is a persistent myth that Java is slow or people don't understand that the Hotspot JRE contains an optimizing compiler that produces native machine code. 
|
|
|
|
gouessej
|
 |
«
Reply #20 - Posted
2013-08-16 22:31:30 » |
|
I'm not surprised by this benchmark. Java can be a bit faster than C/C++ since 2004 except with some specific kinds of algorithm, for example those benefiting the most of the vectorization (SIMD) but the shader language compiler Decora (JavaFX) uses SSE 
|
|
|
|
|