分享

变量的访问,通过类或直接定义的差距

 Harrison.Ding 2012-06-28
#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

                                    

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约