楼主每次想查看函数时间都要去看之前的项目很是恼火,在此记录以加强记忆。
#include <time.h>
clock_t t1,t2;
t1 = clock();
Test_something();
t2 = clock();
cout<<"Test_something use " << static_cast<double>((t2 - t1) / CLOCKS_PER_SEC) << endl;
注意:
- <time.h>是必要头文件。
- 用t2与t1的时间差来估算Test_something()函数所需时间,除以CLOCKS_PER_SEC将毫秒转化为秒。
- <time.h>头文件 自带 #define CLOCKS_PER_SEC 1000。
- Test_something()函数是需要测试时间的自定义函数。