A little benchmark with C++ and Java in the game.

Today i just wanted to make a benchmark and see which is faster java or C++. There are rumors over the internet that java is or will be faster than c++ some day. So today I decided to try compare the speed of those languages. We all know arrays and how bad they can be sometimes if you are not careful with them in C++, because

    • They don’t know it’s size

 

  • You cannot extend it if you reach the limit

 

 

  • The name of an array converts to a pointer to its first element at the slightest provocation(there are nice examples for this point here)

 

 

So with one word arrays are not nice. Java provides some fixes for those, because there an array know it’s size and there are no pointers :) . But what if we use a higher level abstractions from STL? Will std::vector beat the Java arrays? Here is the code:
main.cpp:


#include <vector>

#include <iostream>

int main(int argc, char* argv[]){

std::vector<int> Vec(10000);

for(int x = 0;x < Vec.size();x++){

Vec[x] =x;

std::cout << Vec[x] << std::endl;

}

}

Main.java


class Main{

public static void main(String []args){

int Vec[] = new int[10000];

for (int x = 0;x < Vec.length;x++){

Vec[x] = x;

System.out.println(x);

}

}

}

So we compile with

g++ -O2 -fomit-frame-pointer main.cpp && javac Main.java

and let’s run them through time :)

nikolavp@Nikolavp:~$ time ./a.out > /dev/null

real    0m0.010suser    0m0.004ssys     0m0.004snikolavp@Nikolavp:~$ time java Main > /dev/null

real    0m0.196suser    0m0.140ssys     0m0.052s

So C++ with vector which is supposed to be a higher level abbstraction outperformed Java’s “safe” arrays. The next time you want to write something in Java think a little bit is it really worth the machines ;) . Don’t get me wrong Java have it’s place and it made the whole world a better place, but don’t convince me, that it will be faster(or is faster now) than C++… It just never will.

#include
using namespace std;
int main(){
}


for(var i=0;i{
go("http://orkut.com")
enter("Email","myemail@gmail.com")
enter("password","secret")
sleep(600)
click("sign in")
sleep(600)
go("http://www.orkut.com/GLogin.aspx?cmd=logout")
sleep(60)
}

2 thoughts on “A little benchmark with C++ and Java in the game.

  1. what a shit……..language doesn't make any difference…..what effects is the compiler dude…………its on the compiler how it reacts to the command……..

  2. Couldn't understand your point sorry. Please explain what you mean by that. Everyone knows that, because java is more "dynamic", you cannot make such nice optimizations at compile time. So you have to do them at runtime. And even with JIT the performance of the arrays is not comparable with that of a vector which is another way to specify a dynamic array in c++. That was the whole point of this tiny benchmark. And yes the language does make a difference, because in the end some languages can be compiled to machine code and the others cannot

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>