C language - Calculate the running time of a certain code

  1. #include<stdio.h>  
  2. #include<process.h>  
  3. #include<time.h>//time.h header file  
  4.   
  5. intmain  ()  
  6. {  
  7.   
  8.     int   begin, end; //define start and end flags  
  9.   
  10.         begin=clock(); //Start timing  
  11.    
  12.     end=clock(); //end timing  
  13.     printf( "%d\n" , end-begin); //The difference is time, in milliseconds  
  14.   
  15.         system("pause");  
  16.     return 0;  
  17. }  


Use the clock() function included in time.h to calculate the running time of a piece of code.

The time.h header file has a lot more to explore.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325807261&siteId=291194637