记录一些快速提高C++程序性能的方法。因为运行环境存在差异,样例CPU耗时结果仅供参考。
运行机器CPU信息:
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Address sizes: 46 bits physical, 48 bits virtual
Byte Order: Little EndianCPU(s): 2
CPU MHz : 2500
Caches (sum of all):
L1d: 32 KiB (1 instance)
L1i: 32 KiB (1 instance)
L2: 1 MiB (1 instance)
L3: 33 MiB (1 instance)
编译与CPU耗时统计方式:
//gcc version 12.2.0
//g++ -std=c++11 -g -O2 -o test test.cpp
int main()
{
clock_t start = clock();
//测试函数
double elapsedTime = static_cast<double>(clock()-start) / CLOCKS_PER_SEC;
std::cout << elapsedTime << "s" << std::endl;
return 0;
}