C言語はどのようにプログラムの運行時間を計算します

1853 ワード

01.C/C++       clock(),           clock_t。 MSDN ,   clock      :  
02.  
03.clock_t clock( void );  
04.  
05.       “        ” “     clock()  ”    CPU      (clock tick) , MSDN        (wal-clock)。  clock_t            , time.h   ,           :  
06.  
07.#ifndef _CLOCK_T_DEFINED   
08.typedef long clock_t;  
09.#define _CLOCK_T_DEFINED   
10.#endif   
11.  
12.   ,clock_t       。 time.h   ,        CLOCKS_PER_SEC,                   ,     :  
13.  
14.#define CLOCKS_PER_SEC ((clock_t)1000)   
15.  
16.           (1  ),  clock()        1。      ,       clock()/CLOCKS_PER_SEC              :  
17.  
18.void elapsed_time()  
19.{  
20.printf("Elapsed time:%u secs.
",clock()/CLOCKS_PER_SEC); 21.} 22. 23. , clock : 24. 25.#include “stdio.h” 26.#include “stdlib.h” 27.#include “time.h” 28. 29.int main( void ) 30.{ 31. long i = 10000000L; 32. clock_t start, finish; 33. double duration; 34. /* */ 35. printf( "Time to do %ld empty loops is ", i ); 36. start = clock(); 37. while( i-- ) ; 38. finish = clock(); 39. duration = (double)(finish - start) / CLOCKS_PER_SEC; 40. printf( "%f seconds
", duration ); 41. system("pause"); 42.} 43. 44. , : 45. 46.Time to do 10000000 empty loops is 0.03000 seconds 47. 48. 1 , 1 , CLOCKS_PER_SEC , , ? , 。 C/C++ , 。

http://blog.csdn.net/querdaizhi/article/details/6925156