Speed performance of deployed application: Linux vs Windows
I've developed a small scientific program that performs some mathematical computations. I've tried to optimize it so that it's as fast as possible.
Thanks to help in this forum, I'm almost done deploying it for Windows, Mac and Linux users. But I have not been able to test it on many different computers yet.
Here's what troubles me: To deploy for Windows, I've used a laptop which has both Windows 7 and Ubuntu 12.04 installed on it (dual boot). I compared the speed of the app running on these two systems, and I was shocked to observe that it's at least twice as slow on Windows! I wouldn't have been surprised if there were a small difference, but how can one account for such a difference?
Here are a few precisions:
- The computation that I make the program do are just some brutal and stupid mathematical calculations, basically, it computes products and cosines in a loop that is called a billion times. On the other hand, the computation is multi-threaded: I launch something like 6 QThreads.
- The laptop has two cores @1.73Ghz. At first I thought that Windows was probably not using one of the cores, but then I looked at the processor activity, according to the small graphic, both cores are running 100%.
- Then I thought the C++ compiler for Windows didn't the use the optimization options (things like -O1 -O2) that the C++ compiler for Linux automatically did (in release build), but apparently it does.
I'm bothered that the app is so mush slower (2 to 4 times) on Windows, and it's really weird. On the other hand I haven't tried on other computers with Windows yet. Still, do you have any idea why the difference?
Additional info: some data...
Even though Windows seems to be using the two cores, I'm thinking this might have something to do with threads management, here's why:
Sample Computation n°1 (this one launches 2 QThreads):
Sample Computation n°2 (this one launches 3 QThreads):
Sample Computation n°3 (this one launches 6 QThreads):
PC1-windows = my 2 cores laptop (@1.73Ghz) with Windows 7
PC1-linux = my 2 cores laptop (@1.73Ghz) with Ubuntu 12.04
PC2-linux = my 8 cores laptop (@2.20Ghz) with Ubuntu 12.04
Note: Of course, it's not shocking that PC2 is faster. What's incredible to me is the difference between PC1-windows and PC1-linux. I've also tried running the program on a recent PC (4 or 8 cores @~3Ghz, don't remember exactly) under Mac OS, speed was comparable to PC2-linux (or slightly faster).
During the 90' Linux kernel was greatly optimised to fit multi-core supercomputers, so thread management here is really superb. I think Thiago is working on speeding threads even more (Qt-wise) on both linux and Windows. I don't remember the bottom line, but it will hit the repos for either Qt 5.0 or 5.1.
But it is also possible that the reason behind this performance difference is something else.
Okay, I'll tell you if I find something. Thanks for your answer.
Working on the issue a little bit more and following suggestions from the stackoverflow guys, I found some answers to my "problem". Here comes:
I wanted to determine whether the difference in speed really had something to do with threads or not. So I modified the program so that the computation only uses 1 thread, that way I am pretty much comparing the performance on "pure C++ code". It turned out that now Windows was only slightly slower than Linux (something like 15%). So I guess that a small (but not unsignificant) part of the difference is intrinsic to the system, but the largest part is due to threads management.
I tried building the application for Windows with the compiler for Microsoft Visual Studio (MSVC), instead of MinGW. And suprise, the computation (with all the threads and everything) was now "only" 20% to 50% slower than Linux! I think I'm going to go ahead and be content with that. I noticed that weirdly though, the "pure C++" computation (with only one thread) was now even slower (than with MinGW), which must account for the overall difference. So as far as I can tell, MinGW is slightly better than MSVC except that it handles threads like a moron.
So, I'm thinking either there's something I can do to make MinGW (ideally I'd rather use it than MSVC) handle threads better, or it just can't. I would be amazed, how could it not be well known and documented ? Although I guess I should be careful about drawing conclusions too quickly, I've only compared things on one computer (for the moment).
Edit: I'll take advantage of this thread to ask a question about licensing, I have a hard time getting the subtleties of this. Can I release my program for Windows under the GPL? Even if I use the MSVC compiler?
The performance is closely related to the MinGW version (the higher usually the better, the 4.4 which comes with Qt4 is heavily outdated and has known performance issues) and threading model used (posix or win32).
Yes, you can use MSVC to release commercial (and non-commercial) open- and closed-source applications.
Have you tried profiling your application to find out where it actually loses performance?
Creation/destruction of threads is really well optimized on Linux. If you are using short-lived threads then a thread pool might help by avoiding many of those constructions/destructions.
Basically you implement a pool where you get threads from, have them do something and then return them to the pool for reuse later. I've seen dramatic speedups from using such pools before.
I'm having a really hard time trying to figure out and/or installing the "latest version of Qt". A little help or advice would be very welcome.
On Windows, I have installed the latest SdK. But then I realized the Qt version it used was 4.8.1, whereas the latest one is 4.8.3. So I downloaded the (minGW) 4.8.3 libraries for Windows, ran it (it's an exe), that installed Qt in C:\Qt\4.8.3. Then in the options of Qt creator (Build & Run / Version of Qt) , I added a version of qt by selected the qmake.exe in the appropriate subfolder.
Problems and questions:
How do I know the latest version of MinGW (the one I just installed?) is used? If I go in "tool chains" (still in Qt creator options / Build & Run), it says mingw-32-g++.exe is fetched in the C:\QtSDK... folder, which seems suspicious? On the other hand, I can't find any other minGW on my hard drive..
Do I also need to worry about the version of g++ used or something?
Now the build of my app fails, says it refuses to handle exceptions (and suggests to compile with an option that doesn't work). Why is that happening? By the way, it doesn't matter all that lot, I only use exceptions to throw errors (although I did spend a lot of time managing my exceptions "properly). Do you think I should permanently get rid of exceptions?
Even if I comment all the "throws" to avoid this, I get another error: something like "can't find file qrc_resources.cpp", which doesn't happen usually. Why?
Furthermore, it seems that now if I build & run my app with the previous configuration (Qt 4.8.1) then it's much slower than before (but I've not investigated that at all, so don't be concerned about that)
On Linux (Ubuntu), I have installed Qt (and then Qt creator 2.5.0) by installing lib-qt4-dev. But same thing, that only incorporates Qt 4.8.1. So I downloaded the sources of 4.8.3 (for Linux 64bits), extracted them in my home folder, and I built them. Then I selected the Qt version in Qt Creator just like above. It seems to work, pretty much, except that
now my windows, and buttons, and everything look terribly "basic" in an ugly way. What's going on?
I must admit, the time and effort I've spent trying to deploy my app is giving my nerves a hard time. Maybe today's (and yesterday's) lesson is that I should just use the Qt version provided with my "Qt environment"? (but if I want to use the MinVC compiler...)
Thanks for helping!
You said you installed a new version of Qt... did you install a new version of mingw in addition to that? Qt does not contain a mingw, only the Qt SDK does. The old one should be fine, but a newer one might produce faster code, so you might want to grab a new copy of mingw from the net.
Well, I told you what I did and didn't do in my last post, and what I don't know/understand how to do.
The mingw Qt was build using mingw, it does not contain mingw. If you did not download mingw separately then you won't have a new mingw compiler to install in creator.
My experience it the same. I had a Qt program running in my laptop, core i5, with a dual boot, Windows 7 64b versus Linux Mint 64b. My program would make some math calculations and then draw a graph in QGLWidget. It took about 7ms in linux versus 16ms in Windows. I was using Qt SDK with mingw in windows.
[quote author="seubri" date="1350448721"]On Windows, I have installed the latest SdK. But then I realized the Qt version it used was 4.8.1, whereas the latest one is 4.8.3. So I downloaded the (minGW) 4.8.3 libraries for Windows, ran it (it's an exe), that installed Qt in C:\Qt\4.8.3. Then in the options of Qt creator (Build & Run / Version of Qt) , I added a version of qt by selected the qmake.exe in the appropriate subfolder.
Problems and questions:
How do I know the latest version of MinGW (the one I just installed?) is used? If I go in "tool chains" (still in Qt creator options / Build & Run), it says mingw-32-g++.exe is fetched in the C:\QtSDK... folder, which seems suspicious? On the other hand, I can't find any other minGW on my hard drive..[/quote]That is fine, nothing suspicious at all. The Qt library (i.e. headers + DLLs) and MinGW are separate components -- the latter is bundled in the SDK, but is not in the Qt 4.8.3 library installer you downloaded.
MinGW provides the compiler (i.e. GCC). The MinGW that comes bundled in the SDK contains GCC 4.4, which is the latest version supported by Qt. In fact, since you've installed pre-built Qt 4.8.3, you NEED to use GCC 4.4 -- the latest MinGW contains GCC 4.7, which is binary-incompatible with your Qt 4.8.3 DLLs. If you want to use GCC 4.7, you need to get it from the MinGW site (http://www.mingw.org/wiki/Getting_Started ) AND THEN use it to build Qt 4.8.3 DLLs from source.
[quote]# Do I also need to worry about the version of g++ used or something?[/quote]GCC 4.4 definitely works. GCC 4.7 definitely doesn't work. Not sure about GCC 4.5 or 4.6.
Edit: "Works" = "works with the pre-built libraries downloaded from the website"
[quote]# Now the build of my app fails, says it refuses to handle exceptions (and suggests to compile with an option that doesn't work). Why is that happening? By the way, it doesn't matter all that lot, I only use exceptions to throw errors (although I did spend a lot of time managing my exceptions "properly). Do you think I should permanently get rid of exceptions?[/quote]Hmm... I don't use exceptions myself so I'm not sure, but I'm guessing that the pre-built libraries had exceptions disabled (with the -no-exceptions flag during configuration). I guess your options are:
Use your downloaded Qt 4.8.3, remove exceptions from your code
Compile Qt 4.8.3 yourself with exception support, continue using exceptions in your code.
In general, whether exceptions should be used or not is a decision up to the programmer; there's plenty of debate on the topic.
[quote]# Even if I comment all the "throws" to avoid this, I get another error: something like "can't find file qrc_resources.cpp", which doesn't happen usually. Why?[/quote]Could this be the same issue as http://qt-project.org/forums/viewthread/21032/ ?
[quote]# now my windows, and buttons, and everything look terribly "basic" in an ugly way. What's going on?[/quote]Maybe Qt has defaulted to a different style. Try calling QApplication::setStyle() in main(), to pick a style you like.
Apparently I messed up sending my last post. I was thanking Tobias Hunger and john_god for their insights.
JKSH, thank you so much! I really appreciate you taking the time to answer each one of my questions, Things are much clearer now. So, again, thanks!
So, I guess that if I trust Lukas geyer:
"The performance is closely related to the MinGW version (the higher usually the better, the 4.4 which comes with Qt4 is heavily outdated and has known performance issues)"
then I should get the latest MinGW version from their website and build Qt 4.8.3 from the sources on my Windows using it (yuk, but I should be able to do that).
You're most welcome, and good luck with compilation.
Small tip: Since you're compiling for Windows, you need to use the "configure.exe" executable, not the "configure" script, even if you're in MSYS (which is a Unix-like environment for Windows, from the MinGW project)
i made a disco app that draws 450x450 rects in random colors, every 16ms ,
on linux it was supper smooth ! on windows it lagged so much, i tought its problem with my compiling or windows setting but i guess linux is better in speed :P