CC++ 测试程序运行时间

xiaoxiao2021-02-28  17

1. 方法

算法分析中需要对各种算法进行性能测试,下面介绍一种通用的算法运行时间的测试方法。由于只用到标准c语言函数,所以在各种平台和编译器下都能使用。

clock()函数

头文件:ctimestart和end都是clock_t类型 开始计时:start = clock() 结束计时:end = clock()结果(秒):time = (double)(end - start) / CLOCKS_PER_SEC

2. 代码

#include <iostream> #include <ctime> using namespace std; int main() { clock_t start, finish; start = clock(); cout << "Hello World" << endl; finish = clock(); cout << "运行时间:" << (double)(finish - start) / CLOCKS_PER_SEC << "(s)" << endl; system("pause"); return 0; }

运行结果:

转载请注明原文地址: https://www.6miu.com/read-1650347.html

最新回复(0)