C++: Is Java faster than C++?

I am just speculating here and the title of my post is just based on some tests I performed in my spare time; but, according to them, based on dynamic array lists and strings, Java is far faster than C++.

The simple Java program is this:

package jtest;

import java.util.*;

public class Main
{
        public static void testPtrList(int z)
        {
                ArrayList<String> list = new ArrayList<String>();
                int z0 = z * 2000;
                for (int i = z0; i < z0 + 2000; i++)
                {
                  String s = "X-";
                  s += i;
                  list.add(s);
                }
        }
	
        public static void main(String... args)
        {
                long t0 = System.currentTimeMillis();
		
                for (int i = 0; i < 10000; i++)
                {
                  if (i % 1000 == 0) System.out.println(i);	  
                  testPtrList(i);
                }
		
                long tz = System.currentTimeMillis();
                System.out.println("Milliseconds: " + (tz - t0));
        }
}

As you can see, the test is quite simple, calls a method 10000 times and the method creates 2000 strings and adds them into a dynamic array list.

I also implemented the same stuff using STL in C++:

#include <string>
#include <vector>
#include <iostream>

using namespace std;

void testList(int i)
{
  char aux[10];
  
  int z0 = i * 2000;
  vector<string> v;
  for (int i = z0; i < z0 + 2000; i++)
  {
    string s("X-");
    sprintf(aux, "%d", i);
    s += aux;
    v.push_back(s);
  }
}

int main()
{
  for (int i = 0; i < 10000; i++)
  {
    if (i % 1000 == 0) printf("%d\n", i);
    
    testList(i);   
  }
  return 0;
}

The results:

In my box, the C++ program is executed in 15 seconds approximately, but the program executed using the HotSpot JVM, is executed in….

in…

2 SECONDS!!!

I know my test is tiny and it cannot be useful as a real world performance benchmark, but it says some interesting things:

  • There are things where Java outperforms C++ with almost no effort.
  • I implemented the C++ thing using STL; I have also created my own String and Array classes [as optimized as I could] and implemented the same test using them: the performance was similar. My classes were implemented using full pointers [trying to mimic the Java behavior]. So, I think the problem is not in the code, but in the compiler.
  • I replaced g++ to llvm-g++ and the C++ program was executed in about 10 seconds [33% faster than the g++ binary].
  • I implemented a similar thing in C# and tested using Mono, but the thing got executed in 21 seconds; so, Mono is behind Java right now; I have no environment to run such C# program using the .NET Framework [and running it inside a virtual machine is not fair at all], so I cannot infer anything about this.
    No matter if the performance is inferior in other areas, my simple test demonstrates the amazing development of the Java JIT compiler.

Ah! I have also compiled my Java program using gcj [to native code]…. and the execution took 30 seconds approx… so… gcj is behind Java right now too.

Advertisements

3 thoughts on “C++: Is Java faster than C++?

  1. Nice work!

    The std string implementation in c++ is known to be a little slow. Would be interesting to profile this and see if that’s where the cyles are being eaten up.

    Regards

    Rich

  2. #include  <iostream>
    #include  <vector>
    #include  <chrono>
    #include  <algorithm>
    #include  <ext/vstring.h>
    
    // GCC std::string provides copy-on-write but no small string optimisation (SSO)
    // std::string is slow for huge numbers of small strings. vstring has SSO.
    typedef __gnu_cxx::__vstring string;
    
    using std::vector;
    using std::reverse;
    using std::cout;
    using namespace std::chrono;
    
    // sprintf is actually rather slow. Sadly, GCC doesn't provide itoa, utoa, etc:
    // Roll our own utoa; this is more than twice as fast as sprintf
    char* utoa(unsigned x, char* dest, const unsigned base)
    {
    	char *p = dest;
    	do{ *p++ = "0123456789abcdef"[x % base]; x /= base; }while(x);
    	*p = 0;
    	reverse(dest, p);
            return dest;
    }
    
    void testList(int z)
    {
            vector v;
            char aux[16];
    
    	int z0 = z * 2000;
    	for (int i = z0; i < z0 + 2000; ++i)
    	{
    		string s("X-");
    		s += utoa(i, aux, 10);
    		v.push_back(s);
    	}
    }
    
    int main()
    {
    	auto t1 = high_resolution_clock::now();
    
    	for (int i = 0; i < 10000; i++)
    	{
    		if (i % 1000 == 0){ printf("%d\n", i); }
    		testList(i);
    	}
    
    	auto t2 = high_resolution_clock::now();
    	cout << " Milliseconds: " << duration_cast(t2-t1).count() << "\n";
    	return 0;
    }
    

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s