Problem for slow operations in Qt program
-
[quote author="maxoreli" date="1309016523"]
[quote author="marcoB" date="1308953599"]I never tried and don't know it is the right way to go on, but a shortcut to this problem could be put this@QTimer::singleShot(100, this, setupContents());@
in your constructor. You will need to define setupContents() as a slots and then call calculate(); at the end of setupContents() function. [/quote]
[quote author="marcoB" date="1308953599"]I never tried and don't know it is the right way to go on, but a shortcut to this problem could be put this
@QTimer::singleShot(100, this, setupContents());@
in your constructor. You will need to define setupContents() as a slots and then call calculate(); at the end of setupContents() function. [/quote]
I'm trying that,but i have same problems
[/quote]
What is the problem ?
You can add QProgressBar, to indicate the status of setupContents()
-
After singleShot call, your GUI should respond to user, even if I noticed now you put some
@QCoreApplication::processEvents();@
in your loops and that could be enough to achieve what you are looking for.
Has Santosh sayd, try to put a progressBar in your form, it should works -
Hello,Andre please I have difficulty to apply what you saw in your first reply.
Please ,read my code and give an sample of code and how do I do it(speed up my program)
Thanks for advance.[quote author="Andre" date="1309070939"]I find that progressEvents, especially the call without any arguments, is a road to very hard to debug issues. I try to avoid it. [/quote]
-
In the code you posted, you are not using threads; at least not where we can see them.
Your main issue is, that you are starting your intensive code from the constructor of your form. As you have been told before, you should not do that. Instead, you could re-write your constructor to something like this:
@
FormdP0j_Poj::FormdP0j_Poj(QWidget *parent) :
QWidget(parent)
{
setupUi(this);
table=new QTableWidget;
table->setRowCount(5);
table->setColumnCount(40);
// trigger heavy work to be done in a little while
QTimer::singleShot(100, this, SLOT(startHeavyWork()));
}//this needs to be a slot in your declaration
FormdP0j_Poj::startHeavyWork()
{
setupContents();
calculate();
}
@That should at least get your form displayed properly, if not responding well yet.
I am guessing your calculate function may take quite a bit of time, right? Assuming that it is indeed this function that takes most time (and not the setupContents function), let's continue our optimization:
So, the calculate function is a candidate to make run asynchronous. That is: it running should not block your GUI. One problem is, is that your current function will impact the GUI (it creates QTableWidgetItems), and is thus not suited to put in a thread. Doing things in your GUI from another thread is not allowed. Lucky for you, the calculate function is an easy candidate to chop into pieces that can be run one after another, and return to the main loop in between.
How you make the split in blocks exactly, depends on your problem. It might take some experimentation. I have no idea how big the data sets you are working with are, and how much time each iteration may take. My goal would be to make sure you return to the eventloop at least 24 times per second, but again: some experimentation would be needed.
Lets start by re-arranging your calculation a bit: you are looping over the exact same two data structures in both the blocks of your calculation. That is a waste of time. Instead, loop over them only one time, and perform both calculations in one go:
@
//calculation, version 2
void FormdP0j_Poj::calculate()
{
//calcul des coefficients// you seem to be missing some variable declarations. Did you cut those out?
//2 ligne for( int j =1; j < tempS1ixmij_x_q1ij->columnCount() ;j=j+1 ) { c=1; coef=0.; //where are these declared? for( int i=1 ; j < tempS1ixmij_x_q1ij->rowCount() ;i=i+1 ) { if ((tempS1ixmij_x_q1ij->item(i,j) !=0)){ a=tempS1ixmij_x_q1ij->item(i,j)->data(0).toDouble(); coef= coef + a; } if ((tempS2ixmij_x_q2ij->item(i,j) !=0)){ a2=tempS2ixmij_x_q2ij->item(i,j)->data(0).toDouble(); coef2= coef2 + a2; } } curItem=new QTableWidgetItem; curItem->setData(0,coef); table->setItem(3,c,curItem); curItem2=new QTableWidgetItem; curItem2->setData(0,coef2); table->setItem(3,c,curItem2); c=c+1; }
}
@Let us assume that each iteration of your outer loop takes roughly 10 to 40ms. That is, after each iteration, we would like to return to the event loop. This is how you could do that:
@
//calculation, version 3
void FormdP0j_Poj::calculate()
{
//calcul des coefficients
m_working = true; // declared as bool class member
m_workCurrentIteration = 0; //declared as int class member
m_workIterationCount = tempS1ixmij_x_q1ij->columnCount(); //also an int memberQTimer::singleShot(0, this, SLOT(doWorkIteration()));
}//declared as slot in the class declaration
void FormdP0j_poj::doWorkIteration()
{
// you seem to be missing some variable declarations. Did you cut those out?int j = m_workCurrentIteration; c=1; coef=0.; //where are these declared? for( int i=1 ; j < tempS1ixmij_x_q1ij->rowCount() ;i=i+1 ) { if ((tempS1ixmij_x_q1ij->item(i,j) !=0)){ a=tempS1ixmij_x_q1ij->item(i,j)->data(0).toDouble(); coef= coef + a; } if ((tempS2ixmij_x_q2ij->item(i,j) !=0)){ a2=tempS2ixmij_x_q2ij->item(i,j)->data(0).toDouble(); coef2= coef2 + a2; } } curItem=new QTableWidgetItem; curItem->setData(0,coef); table->setItem(3,c,curItem); curItem2=new QTableWidgetItem; curItem2->setData(0,coef2); table->setItem(3,c,curItem); c=c+1; }
//prepare for the next iteration
++m_workCurrentIteration;
if (m_workCurrentIteration < m_workIterationCount) {
//this is the trick: we schedule another iteration to be calculated if we need to do more work
QTimer::singleShot(0, this, SLOT(doWorkIteration()));
} else {
m_working = false;
workDone();
}
}void FormdP0j_poj::workDone()
{
// this method is called when all the iterations are done. You might want to update the GUI to reflect that the calculation is done.
}
@A method like this will make sure you return to your eventloop regulary, thus giving your GUI the chance to update. Note that it will not make it run faster. In fact, it will be slower. But it will keep your users more happy. Using a thread might make your code faster, as your code can potentially use more cores in a multi-core machine. However, if you want to use threads, you can not interweave creating QTableWidgetItems into the calculation code. Instead, you'd have to store the results elsewhere. If you want to go that route, I think using QtConcurrent would be a feasable way to go.
disclaimer: all code presented here has just been typed into the editor of the forum. It is not tested, and only intended as an example.
-
The new operator is a very expensive operation to call in a loop. You may get a reasonable improvement by calculating how many QTableWidgetItem's you actually need and new'ing them all in one go. Then in you loop just set the properties of each item as you do now.
Using some sort of profiling tool such as valgrind (on Linux) will tell you where to focus your optimisation efforts.
-
Now,I remove new's operator in my loop,however ,program is slower
[quote author="ZapB" date="1309088968"]The new operator is a very expensive operation to call in a loop. You may get a reasonable improvement by calculating how many QTableWidgetItem's you actually need and new'ing them all in one go. Then in you loop just set the properties of each item as you do now.Using some sort of profiling tool such as valgrind (on Linux) will tell you where to focus your optimisation efforts.[/quote]
-
that's my new method setupContents()
@
void FormdP0j_Poj::setupContents()
{FormS0ixmij_x_q0ij *matrixS0ixmij_x_q0ij=new FormS0ixmij_x_q0ij; FormS1ixmij_x_q1ij *matrixS1ixmij_x_q1ij=new FormS1ixmij_x_q1ij; FormS2ixmij_x_q2ij *matrixS2ixmij_x_q2ij=new FormS2ixmij_x_q2ij; tempS0ixmij_x_q0ij= matrixS0ixmij_x_q0ij->getTable(); tempS1ixmij_x_q1ij= matrixS1ixmij_x_q1ij->getTable(); tempS2ixmij_x_q2ij= matrixS2ixmij_x_q2ij->getTable();
// QCoreApplication::processEvents();
QVariant variant; //QTableWidgetItem * curItem=new QTableWidgetItem; progressDialog.setRange(0,tempS0ixmij_x_q0ij->columnCount() ); progressDialog.setMinimumDuration(0); if( (tempS0ixmij_x_q0ij!=0) && (tempS1ixmij_x_q1ij !=0) && (tempS2ixmij_x_q2ij !=0) ) { //copie de l'entete int c=1; table->setItem(1,0,new QTableWidgetItem(QString("dP0j/P0j"))); table->setItem(2,0,new QTableWidgetItem(QString("dP1j/P1j"))); table->setItem(3,0,new QTableWidgetItem(QString("dP2j/P2j"))); int nbCols=tempS0ixmij_x_q0ij->columnCount() ; //that's there!! QTableWidgetItem *cur=new QTableWidgetItem[nbCols]; for(int j=1; j < nbCols ;j=j+1) { if( tempS0ixmij_x_q0ij->item(0,j)!=0) { //curItem=new QTableWidgetItem; variant= tempS0ixmij_x_q0ij->item(0,j)->data(0); cur[j].setData(0,variant); table->setItem(0,c,&cur[j]); c=c+1; } progressDialog.setValue(j); progressDialog.setLabelText(tr("Processing ...Please wait")); } progressDialog.setValue(nbCols); table->resizeColumnsToContents(); table->resizeRowsToContents(); horizontalLayout->addWidget(table); }
}
@Hello Andre ,how are you. I tried your codes,that's Ok.As you said,my program is always slower.
Now,I decide to remove QTableWidgetItem,I will find a manner to store the results.So ,I want to use Qthreads,we suppose that QTableWidgetItem is not present in code.What will be Code.
Thanks For Advance and excuse me for disturbance... -
-
[quote author="maxoreli" date="1309162036"]Ok ,I'm trying something,and i reply you once time over.
And about the preview code,is it good?
[quote author="Andre" date="1309161441"]Sorry, I am not going to write it all out for you again. You'll have to do some work yourself too.
[/quote][/quote]
It might be my limited understanding of the English language, but I have no clue what you are asking me here. Could you rephrase, please? -
I ask you if the preview code on QTableWidgetItems is correct for best performance!!!
[quote author="Andre" date="1309162192"]
[quote author="maxoreli" date="1309162036"]Ok ,I'm trying something,and i reply you once time over.
And about the preview code,is it good?
[quote author="Andre" date="1309161441"]Sorry, I am not going to write it all out for you again. You'll have to do some work yourself too.
[/quote][/quote]
It might be my limited understanding of the English language, but I have no clue what you are asking me here. Could you rephrase, please?[/quote]
-
[quote author="maxoreli" date="1309163670"]I ask you if the preview code on QTableWidgetItems is correct for best performance!!!
[/quote]First of all, there is no need to scream at me (three exclamation points). I'm trying to help you, free of charge, but I'll only do that as long as I think it is fun to do so. Getting shouted at does not qualify as fun for me.
Second, I don't get which part of your code you are referring to with "preview code". Do you mean the FormdP0j_Poj::setupContents() code you posted above?
In that case: I guess this is not optimal, no. One obvious thing is, is that you are setting the progress dialog label over and over again. Much worse is, that you modify data in the view on every iteration. That means re-draws, and thus slowdowns. You need to look at using something different than QTableWidget for your viewing needs. Why don't you just store your data in a QVector (or something like that), and create your own QAbstractTableModel-based model class on top of that. Then, you can manipulate the data in a very efficient way, and only after processing, you construct a model to use in visualization on top of it. That would lead to only a single UI update, instead of many.
-
Excuse me so much for (three exclamation points).I dont want to hurt you.
Thanks for your forgiveness..[quote author="Andre" date="1309164380"][quote author="maxoreli" date="1309163670"]I ask you if the preview code on QTableWidgetItems is correct for best performance!!!
[/quote]First of all, there is no need to scream at me (three exclamation points). I'm trying to help you, free of charge, but I'll only do that as long as I think it is fun to do so. Getting shouted at does not qualify as fun for me.
Second, I don't get which part of your code you are referring to with "preview code". Do you mean the FormdP0j_Poj::setupContents() code you posted above?
In that case: I guess this is not optimal, no. One obvious thing is, is that you are setting the progress dialog label over and over again. Much worse is, that you modify data in the view on every iteration. That means re-draws, and thus slowdowns. You need to look at using something different than QTableWidget for your viewing needs. Why don't you just store your data in a QVector (or something like that), and create your own QAbstractTableModel-based model class on top of that. Then, you can manipulate the data in a very efficient way, and only after processing, you construct a model to use in visualization on top of it. That would lead to only a single UI update, instead of many. [/quote]