Qt君最近感觉qDebug相对于printf打印感觉有些慢,但又没有证据,于是闲着就写下qDebug,std::cout,printf的性能表现咯。注:测试数据仅供参考。
0x00 测试环境
环境 |
参数 |
CPU |
i5-8250U |
内存 |
8G |
操作系统 |
Windows@64位 |
Qt版本 |
Qt 5.12.1 |
编译器 |
MSVC2017@64位 |
0x01 数据呈现
通过使用qDebug,std::cout,printf在1秒内打印的字符串数据。
|
debug版本(次/秒) |
release版本(次/秒) |
qDebug |
38317 |
60923 |
std::cout |
382890 |
372696 |
printf |
432606 |
386663 |

0x02 数据分析
- 性能表现:
printf > std::cout > qDebug
;
- qDebug()相对于std::cout和printf差距过大(6~10倍);
- std::cout与printf数据基本一致;
-
std::cout与printf的debug与release差距不大,甚至有debug比release快的现象(可能受实验环境影响)。
0x03 结论
- qDebug比std::cout和printf慢,高频调用有可能影响系统时延;
- 性能均衡推荐选用std::cout;
- 追求性能选用printf。
0x04 测试程序
#include <QDebug>
#include <QElapsedTimer>
#include <iostream>
/* 注:单独打开某个宏测试 */
//#define TEST1
//#define TEST2
//#define TEST3
int main(int argc, char *argv[])
{
#ifdef TEST1
{
QElapsedTimer t;
qint64 it = 0;
t.start();
while (t.elapsed() < 1000) {
qDebug() << "Test1";
it++;
}
qDebug() << "Test1: " << it;
}
#endif
#ifdef TEST2
{
QElapsedTimer t;
qint64 it = 0;
t.start();
while (t.elapsed() < 1000) {
std::cout << "Test2" << std::endl;
it++;
}
std::cout << "Test2: " << it;
}
#endif
#ifdef TEST3
{
QElapsedTimer t;
qint64 it = 0;
t.start();
while (t.elapsed() < 1000) {
printf("Test3\n");
it++;
}
printf("Test3: %lld\n", it);
}
#endif
return 0
}
0x05 测试数据(各10次)
qDebug:
38310 38452 39416 38420 38962 38385 39293 38814 34178 38946
std::cout:
389512 397234 378168 367970 366371 364401 405547 405992 365863 387846
printf:
468310 423937 480598 385025 490155 489473 373419 397995 445099 372054
qDebug:
60779 60710 59450 59685 63298 61044 59788 61167 61822 61495
std::cout:
352541 358754 377001 380487 397576 362145 333757 413027 416352 335320
printf:
468310 329729 333142 320171 333825 330411 471041 473771 468310 337921
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。