Code runs very slow with new compiler

Hi!
I wrote a very simple code to kinda stress test my PC, couldn't think of anything so I just thought to make 1 000 000 random MD5 hashes. It works very well for me.

You can see the code here: http://pastebin.com/ER5uwL2i
MD5 library: http://www.zedwood.com/article/cpp-md5-function

Now the problem is that I wanted to try out C++11 (just to mess around with threads... I know, not the best reason).
I have always used Bloodshed's DevC++ (GCC 3.4.2, 32-bit I think), so I tested out the code with it and had no problems, I was even very satisfied - 1kk MD5 hashes in 10 seconds.

Wanted to see if I can make it faster with threads, but I needed C++11 (didn't want to use pthreads). After the first search - Orwell's DevC++ is what I need (TDM-GCC 4.8.1 64-bit). The same simple code executes much slower - 1kk MD5 hashes in 34 seconds.

That is a problem, I tried to mess with compiler options, such as:
-std=C++11
-m64
-O2/Os (and others)
-march=sandy (since I have Intel i5-2430M)
-mavx/MMX/3Dnow
-s
-mwindows (don't need the console)
But none of these options made any difference.
Here is my makefile (if it helps): http://pastebin.com/wGydN9h8

What could be causing this huge performance difference?
You forgot to show us the code you used for C++11.
@ LB

LOL

Anyways, what he was saying was that you aren't utilizing C++11 features at all. If I were you, I would update my IDE (Visual Studio 2013 is hands down the best IDE imho).

_______________

The reason why it's so slow is that you're constructing a string every single function pass, and you're also _copying_ the entire string object each pass as well. If I were you, I would look into references and pointers.
Last edited on
Like I said before, the same code was used for C++11.

[EDIT]
I know I'm not using any of C++11 features right now.
I did write my code with threads (most useless threads you will ever see - 500k hashes on each thread) and I noticed that the code is very slow (about the same execution time - 35 seconds). So I decided to strip all the thread functions and use the same code that I used before. From there I saw the difference. I would like to fix this problem before I go any further.
I understand that it is pointless to use C++11 with this code, but why shouldn't I use the most "recent" compiler?

I like DevC++ because it is small. I can start it up in few seconds, VS takes a lot more time.
I had VS2010 a while ago, it had too many options, I don't need that many (though the debugger is really useful). Since I don't have much experience with C++, then I should stick with the basic IDE.


About those pointless string creations...
Thats the point, do as much useless things as possible.
That was the first thing that came into my mind to make the application more intense.
I did try to remove all string related code, leaving just this:
1
2
3
   for(int i = 0; i<1000000; i++) {
           md5("aaa");     
   }   

But that doesn't even change anything, still over 30 seconds.
Last edited on
Okay, if you don't like Visual Studio because of it's size, then use CodeBlocks. It's much better than DevC++, and it's modern.

Can you copy and paste your new code?
Last edited on
I just tried out VS2013 Express. It seems to be a lot faster than VS2010 Ultimate(I think).

My only guess would be that the MD5 library is not meant for C++11.
With the same code I got an error about sprintf in the MD5 library (I tried to fix it, but got tired, doesn't really matter). Tried a different library: http://create.stephan-brumme.com/hash-library/

But that wasn't really the problem either.
I got a bit better results - 21 seconds.
With no clue what to do, I simply started to try to "improve" the code with tips you gave about references and pointers. Couldn't really think of anything useful so I simply created a global char [32] and only changed each letter in the array.
And suddenly I was back where I started - execution time was 10 seconds. Added some threads and success - 8 seconds.

So I guess the problem could be that older gcc versions did really optimize multiple string constructions, newer gcc (or whatever VS uses) doesn't do that for you. That's just my guess.

Anyways, the whole idea of this useless script was to prove my self that I can make a simple webpage in C++ that is faster than PHP.
Assuming that I can strip everything down to bare minimum with C++, I can avoid loading useless modules in memory (what does PHP do).

If anyone still wishes to see the C++ code: http://pastebin.com/mjn1ziFX
Here is the PHP code too: http://pastebin.com/a1GWSyhK

C++ execution time (average): 8 seconds
PHP execution time (average): 14 seconds

I know this isn't the best way to test out performance, but just a small project to see why writing in C++ is better.

Thanks for the help!
Last edited on
regdude wrote:
My only guess would be that the MD5 library is not meant for C++11.
I highly doubt this. More likely is that some compiler optimizations are currently disabled in C++11 mode because they are not ready for C++11. But it still doesn't make sense.
Last edited on
Topic archived. No new replies allowed.