Problem for slow operations in Qt program
-
wrote on 26 Jun 2011, 11:49 last edited by
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.
-
wrote on 27 Jun 2011, 07:05 last edited by
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]
-
wrote on 27 Jun 2011, 07:11 last edited by
[quote author="maxoreli" date="1309158340"] Now,I remove new's operator in my loop,however ,program is slower
[/quote]
Curious how you did that... -
wrote on 27 Jun 2011, 07:44 last edited by
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... -
wrote on 27 Jun 2011, 07:57 last edited by
Sorry, I am not going to write it all out for you again. You'll have to do some work yourself too.
-
wrote on 27 Jun 2011, 08:07 last edited by
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] -
wrote on 27 Jun 2011, 08:09 last edited by
[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? -
wrote on 27 Jun 2011, 08:34 last edited by
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]
-
wrote on 27 Jun 2011, 08:46 last edited by
[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.
-
wrote on 27 Jun 2011, 09:14 last edited by
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]
26/26