Results 1 to 11 of 11

Thread: Why is QtConcurrent so slow?

  1. #1
    Join Date
    Nov 2009
    Posts
    22
    Thanks
    2

    Default Why is QtConcurrent so slow?

    Greetings.

    I'm very new to QtConcurrent, and I have very little experience in threading in general. I went over some examples today in the Qt docs, and I can't figure out why my multi-threaded code runs slower than the single-threaded version.

    This code is basically a modified version of what I found in the docs.

    Qt Code:
    1. #include <QThread>
    2. #include <QApplication>
    3. #include <qtconcurrentmap.h>
    4. #include <iostream>
    5. #include <QtConcurrentRun>
    6. using namespace std;
    7.  
    8. int Func(const int &n){
    9.  
    10. //Some random calculations...
    11. int a = rand() % 3, b = rand() % 4, c = rand() % 5;
    12. int x = a * a, y = b * b, z = c * c;
    13. int u = a + x, v = b + y, w = c + z;
    14. int d = u - v * w;
    15. if( d > 0 ){
    16. d = a / x / u;
    17. }else{
    18. d = b / y / v;
    19. }
    20. for(int i = 1; i < 10; ++i){
    21. for(int j = 1; j < 30; ++j){
    22. d = rand() % 5 + 3;
    23. d /= 2;
    24. d *= rand() % 4;
    25. }
    26. }
    27. return d * n;
    28. }
    29.  
    30. int main(int argc, char *argv[]){
    31.  
    32. QApplication app(argc, argv);
    33. const int SIZE = 500000;
    34. QVector<int> data;
    35.  
    36. for(int i = 0; i < SIZE; ++i){
    37. data.push_back(i);
    38. }
    39.  
    40. //non-concurrent version
    41. //for(int i = 0; i < SIZE; ++i){ data[i] = Func( data[i] ); }
    42.  
    43. //concurrent version
    44. QtConcurrent::mapped(data, Func);
    45.  
    46. return 0;
    47. }
    To copy to clipboard, switch view to plain text mode 

    I'm running it on a dual core, and the single-threaded version runs in about 0.7s, but the using QtConcurrent it takes over 30.0s. The two cores are utilized, but not at 100%.

    Also, using QtConcurrent::map() I get seg-fault, and I'm not sure why.

    I'm probably doing something wrong, but can anyone tell me why QtConcurrent::mapped() call slows it down so much??

  2. #2
    Join Date
    Sep 2009
    Location
    Tashkent, Uzbekistan
    Posts
    107
    Thanks
    1
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Why is QtConcurrent so slow?

    Look here. Pretty good example.
    Also, utilization of CPU depends on QThread::idealThreadCount(). Usually it is something like 1 thread per core in order to not overload the OS with your task.

    And you have various errors in your app regarding QtConcurrent use. I suggest you to study the examples bundled within Qt distribution. Sure that helps.
    -- Tanuki

    per cauda vel vaculus cauda

  3. #3
    Join Date
    Jul 2009
    Posts
    139
    Thanks
    13
    Thanked 59 Times in 52 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Why is QtConcurrent so slow?

    Qt Code:
    1. if( d > 0 ){
    2. d = a / x / u;
    3. }else{
    4. d = b / y / v;
    5. }
    To copy to clipboard, switch view to plain text mode 
    These are divide by zero errors in your code. After commenting out these lines and testing, the mapped function takes about five times as long (as the direct implementation) on my system!

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Why is QtConcurrent so slow?

    Are you sure you have been testing with the exact code you pasted? It shouldn't work at all as QtConcurrent::mapped() is a non-blocking function so returning 0 immediately should cause your program to quit (and my tests seem to cofirm that).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Nov 2009
    Posts
    22
    Thanks
    2

    Default Re: Why is QtConcurrent so slow?

    wysota, yes I've been testing with the same exact code. It does run and I started getting errors when I raised the SIZE. I fixed the errors by replacing the last three lines with this:
    Qt Code:
    1. QFuture<void> res = QtConcurrent::map(data, Func);
    2. res.waitForFinished();
    3.  
    4. return 0;
    To copy to clipboard, switch view to plain text mode 
    I do monitor it during run-time with top, and both cores are used, but they never rise above 50%. They usually hover around 30-40%. The single-threaded version runs on one core, but at 100%.

    numbat, I removed the if-else block just so there are no errors in the code, but it still runs several times slower than the single-threaded version. About ten times slower to be exact.

    Tanuki-no, QThread::idealThreadCount() returns 2 for my system, which is correct.

  6. #6
    Join Date
    Nov 2009
    Posts
    22
    Thanks
    2

    Default Re: Why is QtConcurrent so slow?

    I just tested a simple pthread program that creates two threads. I get similar results with it as I did with Qt. The single-threaded version runs about 10 times faster than the multi-threaded version. Does this mean there is something wrong with my system?

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Why is QtConcurrent so slow?

    What does QThread::idealThreadCount() return?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Nov 2009
    Posts
    22
    Thanks
    2

    Default Re: Why is QtConcurrent so slow?

    Quote Originally Posted by wysota View Post
    What does QThread::idealThreadCount() return?
    It returns 2, and I'm running Linux on a dual-core Intel.

    There is some kind of system overhead when running more than one thread, and that causes multi-threaded version of all examples I've tried to run drastically slower than the single-threaded version. I just can't figure out what it is.

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Why is QtConcurrent so slow?

    Is your system doing anything heavy in the background? Does your system have free memory or is it swapping?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #10
    Join Date
    Nov 2009
    Posts
    22
    Thanks
    2

    Default Re: Why is QtConcurrent so slow?

    Quote Originally Posted by wysota View Post
    Is your system doing anything heavy in the background? Does your system have free memory or is it swapping?
    No, my system isn't doing anything heavy, and I have more than enough ram on it. 2GB total, and i'm not running anything else that would exhaust the CPU or memory.

    Question: Does the multi-threaded version (using QtConcurrent::map or mapped) run faster than the single-threaded version on your system??

  11. #11
    Join Date
    Jul 2009
    Posts
    139
    Thanks
    13
    Thanked 59 Times in 52 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Why is QtConcurrent so slow?

    rand() is not thread-safe*. Changing to rand_r and re-running, the multi-threaded version takes slightly less real time, although about 50% extra user time, so there is reasonable overhead using QtConcurrent for very short tasks.

    *Or maybe rand is thread safe and the extra time is waiting for a lock on the state information kept by rand.
    Last edited by numbat; 29th December 2009 at 10:44.

Similar Threads

  1. Very slow repainting ofPixmaps in QGraphicsView images
    By drexiya in forum Qt Programming
    Replies: 3
    Last Post: 21st October 2009, 18:13
  2. Why is drawText so terribly slow?
    By Ntoskrnl in forum Qt Programming
    Replies: 8
    Last Post: 1st August 2008, 19:15
  3. QtConcurrent Performance
    By tomf in forum Qt Programming
    Replies: 3
    Last Post: 15th July 2008, 15:41
  4. QTextEdit slow to insert text
    By thomaspu in forum Qt Programming
    Replies: 4
    Last Post: 10th January 2008, 12:05

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.