Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (521)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (589)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  JAVA faster than C++ now?  (Read 3927 times)
0 Members and 1 Guest are viewing this topic.
Offline phu004

JGO Coder


Medals: 4
Projects: 9
Exp: 10 years


NoSuchPersonException


« 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++  Grin?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
//compiled with g++ 4.6.3
#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  
//compiled with java 1.6.27
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);
   }
}
Offline 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++?
Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 830
Projects: 4
Exp: 16 years


Hand over your head.


« 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!
Legends of Yore - The Casual Retro Roguelike
Offline phu004

JGO Coder


Medals: 4
Projects: 9
Exp: 10 years


NoSuchPersonException


« Reply #3 - Posted 2013-08-16 01:30:58 »

Quote
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 Smiley  (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.
Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 830
Projects: 4
Exp: 16 years


Hand over your head.


« 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
Offline Sindisil

Junior Devvie


Medals: 2
Exp: 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.

Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 830
Projects: 4
Exp: 16 years


Hand over your head.


« 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]; // stack allocated
arr = new double[123]; // heap allocated

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
Offline phu004

JGO Coder


Medals: 4
Projects: 9
Exp: 10 years


NoSuchPersonException


« 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.
Offline Sindisil

Junior Devvie


Medals: 2
Exp: 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!
Offline 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!
Legends of Yore - The Casual Retro Roguelike
Offline 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.

Offline SHC
« Reply #11 - Posted 2013-08-16 03:09:05 »

This stackoverflow question seems interesting.

C++ performance vs. Java/C#

Offline gimbal

JGO Knight


Medals: 25



« Reply #12 - Posted 2013-08-16 07:36:09 »

Does it conclude that Java is now faster than C++  Grin?
1  
//compiled with java 1.6.27


Funny how you say NOW when you use a version of Java that is already balding :s
Offline 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.
Offline concerto49

Junior Devvie





« 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 Shards
Available in Texas, New York & Los Angeles
Need a VPS Upgrade?
Offline davidc

Senior Devvie


Medals: 5
Projects: 2



« 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.
Offline relminator
« Reply #16 - Posted 2013-08-16 10:39:55 »

To the OP: 

Your forgot a delete[].   
Offline Roquen
« Reply #17 - Posted 2013-08-16 11:28:52 »

Sigh:  numbers should be transformed away.  It dead.  See points #1 & #3 above.
Offline pitbuller
« Reply #18 - Posted 2013-08-16 15:19:35 »

Try to remove pointless array. Does this change stats?
Offline Several Kilo-Bytes

Senior Devvie


Medals: 11



« 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[] numbers = new double[32000000];
      double  sum = 0;
      //for(int i = 0; i < 32000000; i++) {
      int i = 32000000 - 1;
      do
      {
        // numbers[i] = 3141592654.987654321/i;
        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. Clueless
Offline 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  Grin

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xFryIx (57 views)
2014-11-13 12:34:49

digdugdiggy (35 views)
2014-11-12 21:11:50

digdugdiggy (29 views)
2014-11-12 21:10:15

digdugdiggy (23 views)
2014-11-12 21:09:33

kovacsa (46 views)
2014-11-07 19:57:14

TehJavaDev (50 views)
2014-11-03 22:04:50

BurntPizza (50 views)
2014-11-03 18:54:52

moogie (65 views)
2014-11-03 06:22:04

CopyableCougar4 (63 views)
2014-11-01 23:36:41

DarkCart (149 views)
2014-11-01 14:51:03
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!