C语言学习:计算时间差

 

 1 #include <io_utils.h>
 2 #include <time_utils.h>
 3 #include <time.h>
 4 
 5 #define PI 3.1415926
 6 
 7 void DoHardWork() {
 8   double sum = 0;
 9   for (int i = 0; i < 10000000; ++i) {
10     sum += i * i / PI;
11   }
12   PRINT_DOUBLE(sum);
13 }
14 
15 int main() {
16   time_t start_time = time(NULL);
17   DoHardWork();
18   time_t end_time = time(NULL);
19   double diff = difftime(start_time, end_time);
20   PRINT_DOUBLE(diff);
21 
22   long_time_t start_time_ms = TimeInMillisecond();
23   DoHardWork();
24   long_time_t end_time_ms = TimeInMillisecond();
25   PRINT_LLONG(end_time_ms - start_time_ms);
26 
27   clock_t start_time_c = clock();
28   DoHardWork();
29   clock_t end_time_c = clock();
30   PRINT_DOUBLE((end_time_c - start_time_c) * 1.0 / CLOCKS_PER_SEC);
31   return 0;
32 }
View Code

 

 

 

posted @ 2023-02-12 13:33  泥古拉斯赵四  阅读(48)  评论(0编辑  收藏  举报