#include <QtCore/QCoreApplication> #include <QDateTime> #include <QString>
class VariableTest { public: QString str; };
int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QDateTime t1; QDateTime t2; long i = 0;
qDebug("Define many times"); t1 = QDateTime::currentDateTime(); for(i = 0; i < 100000000; i ++) { QString str; str = "Hellworld"; str.clear(); }
t2 = QDateTime::currentDateTime(); qDebug("There are %d mseconds to elapse", t1.msecsTo(t2));
VariableTest vt; qDebug("By class member for many times"); t1 = QDateTime::currentDateTime(); for(i = 0; i < 100000000; i ++) { vt.str = "Hellworld"; vt.str.clear(); } t2 = QDateTime::currentDateTime(); qDebug("There are %d mseconds to elapse", t1.msecsTo(t2)); return a.exec(); }
结论: 1. 只操作 clear的效率: Define many times
There are 4469 mseconds to elapse
By class member for many times
There are 625 mseconds to elapse
2. 先Clear后= 的效率: Define many times
There are 34375 mseconds to elapse
By class member for many times
There are 36687 mseconds to elapse
3. 先= 后clear 交换的效率: Define many times
There are 40750 mseconds to elapse
By class member for many times
There are 36500 mseconds to elapse
|