使用QT在windows下高精度测试程序运行时间

#include <QTime>
#include <QApplication>
#include <QMessageBox>
#include <QWidget>
#include <QLabel>
#include <windows.h>
using namespace std;
void caculate()
{
    for(int i=0;i<32323;i++)
    {
        for(int j=0;j<32323;j++) ;
    }
}
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QWidget *w = new QWidget;
    QLabel *label1,*label2,*label3,*label4;
    //QTime mytime = QTime::currentTime ();
    //QMessageBox::warning(w,"test time", mytime.toString());//debug
    LARGE_INTEGER litmp;
    LONGLONG Qpart1,Qpart2,Useingtime;
    double dfMinus,dfFreq,dfTime;

    //获得CPU计时器的时钟频率
    QueryPerformanceFrequency(&litmp);//取得高精度运行计数器的频率f,单位是每秒多少次(n/s),
    dfFreq = (double)litmp.QuadPart;

    QueryPerformanceCounter(&litmp);//取得高精度运行计数器的数值
    Qpart1 = litmp.QuadPart; //开始计时

    caculate(); //待测试的计算函数等
    QueryPerformanceCounter(&litmp);//取得高精度运行计数器的数值
    Qpart2 = litmp.QuadPart; //终止计时

    label1 = new QLabel("Start Countor:"+QString::number(Qpart1,10),w);
    label1->setGeometry(10,10,200,30);
    label2 = new QLabel("Use Countor:"+QString::number((Qpart2-Qpart1),10),w);
    label2->setGeometry(10,50,200,30);
    label3 = new QLabel("End Countor:"+QString::number(Qpart2,10),w);
    label3->setGeometry(10,90,200,30);

    dfMinus = (double)(Qpart2 - Qpart1);//计算计数器值
    dfTime = dfMinus / dfFreq;//获得对应时间,云锡摇床。单位为秒,可以乘精确到微秒级(us)
    Useingtime = dfTime*1000000;
    label4 = new QLabel("Use Time:"+QString::number(Useingtime,10)+" us",w);
    label4->setGeometry(10,120,200,30);
    w->setMinimumSize(300,200);
    w->setMaximumSize(300,200);
    w->show();

    return a.exec();
}

你可能感兴趣的:(windows,测试,Integer,qt,include)