一直很好奇Qt信号槽的速度问题,简单测试了一下,先上代码:
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "CGetCPUClockFrequency.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
signals:
void sigSendStart();
private slots:
void onPrint();
void onbutton1();
void onbutton2();
private:
Ui::MainWindow *ui;
CGetCPUClockFrequency clock;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "MainWindow.h"
#include "ui_MainWindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QPushButton* button1 = new QPushButton(this);
button1->resize(100,200);
button1->setText("btnConnect");
button1->move(100,200);
button1->show();
QPushButton* button2 = new QPushButton(this);
button2->resize(100,200);
button2->setText("btnFunc");
button2->move(400,200);
button2->show();
connect(this,&MainWindow::sigSendStart,this,&MainWindow::onPrint);
connect(button1,&QPushButton::clicked,this,&MainWindow::onbutton1);
connect(button2,&QPushButton::clicked,this,&MainWindow::onbutton2);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::onPrint()
{
qDebug() << clock.getClock();
}
void MainWindow::onbutton1()
{
clock.clockStart();
emit sigSendStart();
}
void MainWindow::onbutton2()
{
clock.clockStart();
onPrint();
}
获取高精度时间
#ifndef CGETCPUCLOCKFREQUENCY_H
#define CGETCPUCLOCKFREQUENCY_H
#include <Windows.h>
class CGetCPUClockFrequency
{
public:
CGetCPUClockFrequency() : Qpart1(0),Qpart2(0),dfMinus(0),dfFreq(0),dfTime(0)
{
}
void clockStart()
{
//获得CPU计时器的时钟频率
QueryPerformanceFrequency(&litmp);//取得高精度运行计数器的频率f,单位是每秒多少次(n/s),
dfFreq = double(litmp.QuadPart);
QueryPerformanceCounter(&litmp);//取得高精度运行计数器的数值
Qpart1 = litmp.QuadPart; //开始计时
}
double getClock()
{
QueryPerformanceCounter(&litmp);//取得高精度运行计数器的数值
Qpart2 = litmp.QuadPart; //终止计时
dfMinus = double(Qpart2 - Qpart1);//计算计数器值
dfTime = dfMinus / dfFreq;//获得对应时间,单位为秒,可以乘1000000精确到微秒级(us)
return dfTime * 1000000;//微秒级us
}
void myUsleepByCPU(double uSleepTime)
{
LARGE_INTEGER litmp;
LONGLONG Qpart1,Qpart2;
double dfMinus = 0,dfFreq = 0,dfTime = 0;//不初始化是一个不好的习惯,而且会有bug
//获得CPU计时器的时钟频率
QueryPerformanceFrequency(&litmp);//取得高精度运行计数器的频率f,单位是每秒多少次(n/s),
dfFreq = (double)litmp.QuadPart;
QueryPerformanceCounter(&litmp);//取得高精度运行计数器的数值
Qpart1 = litmp.QuadPart; //开始计时
while((uSleepTime-dfTime*1000000.000)>0.0000001){
QueryPerformanceCounter(&litmp);//取得高精度运行计数器的数值
Qpart2 = litmp.QuadPart; //终止计时
dfMinus = (double)(Qpart2 - Qpart1);//计算计数器值
dfTime = dfMinus / dfFreq;//获得对应时间,单位为秒,可以乘1000000精确到微秒级(us)
// qDebug()<<"us"<<QString::number(dfTime*1000000.00,'f',9);
}
}
private:
LARGE_INTEGER litmp;
LONGLONG Qpart1,Qpart2;
double dfMinus,dfFreq,dfTime;
};
#endif // CGETCPUCLOCKFREQUENCY_H
看打印结果:(时间单位微秒级)
从左至右 release 信号与槽 release 函数直接调用 Debug 信号槽 Debug 函数直接调用
由结果可以看出,在release模式下,信号槽与函数直接调用没有太大区别,时间基本一至.但是在Debug模式下,函数调用所用时间远小于信号槽
这里是微秒级时间,其大部消耗时间是在qDebug上,若将打印函数为使用cout打印
void MainWindow::onPrint()
{
std::cout << clock.getClock() << std::endl;
}
在release模式下,信号槽打印平均0.5微秒,函数直接调用0.1微秒,
两者结合应该可以肯定信号槽会比函数直接调用方式慢,大概会慢5倍.
此测试不严谨,仅参考,若要准确测试,可写两线程,在各自线程内,测试规定时间内,两种方式会各跑多少次.