C++ vs. Java benchmarks?

Note: I think this should go on the top: This post has been attracting alot of views(which I am very much enjoying). And a many of that think I am wrong to do it in the first place, guys! it’s something that I did in my spare time I have not come to any conclusion other than my optimmized code is faster than the one given by the original author. Who was flaming C++ for being slow. I do not know what content in this post makes my post look like that it was written by someone who says C++ is the only language that should be used. And if you are offended by this post, all I can tell is to read the last paragraph again. This post is not about “what’s the best language evar!”, it is about C++ optimizations and how it is easy to write slow code in C++. And ofcause, the links are given here for a reason read them first.

The internet is full of surprises, today I was searching for a better C++ IDE(I know about KDevelop and how awesome it is, yet there might be something better!). I stumbled upon this conversation,

http://www.cplusplus.com/forum/unices/67839/ and from a link from there this, http://www.cplusplus.com/forum/unices/64290/

Have to say that I was amazed to see them proving C++ slower than Java in the most uninformed way possible! The code has been crafted from a Java perspective and has been implemented in C++. Which is insane because, no one in their right mind would think that the given C++ code is optimal. Unlike in Java where the developer can do very little to optimize the code, C++ has a wide range of optimizations that should be done by the developer. C/C++ is considered not as easy as Java just because of that, it is so much easier to write slower code with it because there are too many ways to get one thing done.

Java is garbage collected, so it does no freeing of memory until its next collection round. This can be a huge problem if memory isn’t preallocated outside loops and again there’s the space-time trade off which any comp. sci. student would know mostly by experience GC systems achieve high performance by killing the space, it eats up as much memory as possible and gives you some performance increase. Given this amount of memory(preallocated) to unmanaged code you will see performance boosts more than you will ever see with a GC, that’s the only thing that I’ve changed in the code that the author claiming C++ is slower has given, I just did a preallocation. This will be faster if everything happened on stack, but that needs a resize of the stack to be run correctly so I used new and delete. shared_ptr was left because the user has claimed its being widely used and needed here(?). He should be informed that there are good garbage collector implementations in C++.

The Java code which is on the post(one similar to the below C++ code) runs in 5.6 seconds on host machine. With following arguments,

-XX:+UseCompressedOops -XX:+DoEscapeAnalysis -XX:+EliminateLocks -XX:+UseBiasedLocking

Host runs Arch Linux, so Java detected that its a server and added -server! Further, the extra arguments had no effect on this machine. And it was Oracle JDK.

#include <vector>
#include <string>
#include <memory>
#include <ctime>
#include <iostream>

void test() {
  const int COUNT = 1000000;

  std::shared_ptr<std::string> *v=(std::shared_ptr<std::string> *) ::operator new(sizeof( std::shared_ptr<std::string>)*COUNT);
  std::shared_ptr<std::string> sp(new std::string("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"));

  for(size_t i = 0; i < COUNT; ++i)
      v[i]=sp;
  delete v;
}

int main() {

    std::clock_t b, e;
    b = std::clock();

    for (int i = 0; i < 1000; ++i)
        test();

    e = std::clock();

    std::cout << "Time: " << (double(e - b)/CLOCKS_PER_SEC) << '\n';
    return 0;
}

Above code just runs on 4.6 seconds. With just -O3.

Memory usage

C++ code: 15 MB

Java code: 320 MB

Java VM only(taken from running a infinite loop): 8 MB

There’s a lot of optimizations one would do to the above C++ code, I just did one and mentioned another. But there is another, which runs even faster!

const int COUNT = 1000000;
char v[COUNT*sizeof(std::shared_ptr<std::string>)];
void test() {
  std::shared_ptr<std::string> sp(new std::string("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"));

  for(size_t i = 0; i < COUNT; ++i)
      memcpy((void*)&v[i*sizeof(std::shared_ptr<std::string>)],(void*)&sp,sizeof(std::shared_ptr<std::string>));
}

And still keeping shared_ptr! This one runs on 3.6 seconds! Original author might disagree with the usage of memcpy and static allocation(specially when Java doesn’t have static allocation), well C++ doesn’t have a GC by default either!

Languages have their differences and there are claims that people should avoid from when one of the languages in question has two levels of complexities one on actual hardware optimization and another on VM code optimization. Not to mention this post is not and was not about what’s the best language it is just a post detailing C++ optimizations.

Advertisements

4 thoughts on “C++ vs. Java benchmarks?

  1. Good post..
    First of all got to say, the one who compared c++ n java and said java is faster is completely dumb. But his effort should be appreciated even though it was a wasteful effort.
    My opinion is comparing both languages is a useless process. Java has its own advantages and disadvantages comparing to c++.
    Btw, your way of optimizing the code is cool. Keep it up.

    Reply
    • Thanks, yes I do give a lot of credit to the original author even though he has used Java style code with C++ which you won’t end up having optimized. I thought it can be done better in C++ side. True, its dumb to compare but it was a good exercise for a lazy day 😀

      Reply
    • Benchmarks created part of “the history” and are part of the history itself. What’s considered factual before may no longer hold true today. Your reasoning is seriously flawed.

      Reply

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