18计算程序的执行时间

1、计算程序的执行时间(单位是秒级)

使用函数time()

 1 #include <iostream>
 2 #include <time.h>
 3 #include <unistd.h>
 4 using namespace std;
 5 
 6 void WasteTime()
 7 {
 8     sleep(5);//秒级睡眠
 9 }
10 
11 int main(void)
12 {
13     time_t t1 = time(NULL);
14     WasteTime();
15     time_t t2 = time(NULL);
16 
17     double calcualte = (double)(t2 - t1);//强制类型转换
18     cout << calcualte << endl;
19 
20     return 0;
21 }

2、计算程序的执行时间(单位是毫秒级)

猜你喜欢

转载自www.cnblogs.com/Long-w/p/9435238.html
今日推荐