C\C++语言中的计时函数

1. <time.h>中函数clock(),返回类型clock_t,精确度,毫秒级别

  1. <span style="font-size:18px;">#include <stdio.h>   
  2. #include <time.h>   
  3. #include <math.h>   
  4.   
  5. void test()   
  6. {   
  7.     int i = 0;   
  8.     int j = 0;   
  9.     double a = 0;   
  10.   
  11.     while (i++ < 1000000)   
  12.         while (j++ < 1000000)   
  13.         {   
  14.             a = sqrt(2.0);   
  15.         }   
  16. }   
  17.   
  18. int main(void)   
  19. {   
  20.     clock_t start, finish;   
  21.     double  duration = 0.0;   
  22.   
  23.     start = clock();   
  24.   
  25.     test();   
  26.   
  27.     finish = clock();   
  28.     duration = (double)(finish - start); //输出单位ms  
  29.     duration = (double)(finish - start) / CLOCKS_PER_SEC; //输出单位为s, //#define CLOCKS_PER_SEC 1000   
  30.       
  31.     printf("%f seconds\n", duration);    
  32.   
  33.     return 0;   
  34. } </span>  
<span style="font-size:18px;">#include <stdio.h> 
#include <time.h> 
#include <math.h> 

void test() 
{ 
	int i = 0; 
	int j = 0; 
	double a = 0; 

	while (i++ < 1000000) 
		while (j++ < 1000000) 
		{ 
			a = sqrt(2.0); 
		} 
} 

int main(void) 
{ 
	clock_t start, finish; 
	double  duration = 0.0; 

	start = clock(); 

	test(); 

	finish = clock(); 
	duration = (double)(finish - start); //输出单位ms
	duration = (double)(finish - start) / CLOCKS_PER_SEC; //输出单位为s, //#define CLOCKS_PER_SEC 1000 
	
	printf("%f seconds\n", duration);  

	return 0; 
} </span>
2. 最精确的计时:QueryPerformanceCounter来查询定时器的计数值,如果硬件里有定时器,它就会启动这个定时器,并且不断获取定时器的值,这样的定时器精度,就跟硬件时钟的晶振一样精确的。
  1. #include <stdio.h>   
  2. #include <stdlib.h>   
  3. #include <windows.h>   
  4. #include <math.h>  
  5.   
  6. void test()   
  7. {   
  8.     int i = 0;   
  9.     int j = 0;   
  10.     double a = 0;   
  11.   
  12.     while (i++ < 1000000)   
  13.         while (j++ < 1000000)   
  14.         {   
  15.             a = sqrt(2.0);   
  16.         }   
  17. }   
  18.   
  19. int main(void)   
  20. {   
  21.     LARGE_INTEGER start;   
  22.     LARGE_INTEGER end;   
  23.     LARGE_INTEGER freq;   
  24.   
  25.     QueryPerformanceFrequency(&freq);   
  26.     QueryPerformanceCounter(&start);   
  27.   
  28.     test();    
  29.   
  30.     QueryPerformanceCounter(&end);   
  31.   
  32.     printf("user time : %.10f seconds\n", (double)(end.QuadPart - start.QuadPart) / (double)freq.QuadPart);   
  33.   
  34.     return 0;   
  35. }   
#include <stdio.h> 
#include <stdlib.h> 
#include <windows.h> 
#include <math.h>

void test() 
{ 
	int i = 0; 
	int j = 0; 
	double a = 0; 

	while (i++ < 1000000) 
		while (j++ < 1000000) 
		{ 
			a = sqrt(2.0); 
		} 
} 

int main(void) 
{ 
	LARGE_INTEGER start; 
	LARGE_INTEGER end; 
	LARGE_INTEGER freq; 

	QueryPerformanceFrequency(&freq); 
	QueryPerformanceCounter(&start); 

	test();  

	QueryPerformanceCounter(&end); 

	printf("user time : %.10f seconds\n", (double)(end.QuadPart - start.QuadPart) / (double)freq.QuadPart); 

	return 0; 
} 

3. <time.h>中函数time(&t), 精确度,秒级别 
功能:取以秒为单位的,从1970年1月1日格林威治时间00:00:00算起的当前时间,并把它存在长整形变量t中,函数返回如前所述的时间秒值。 
  1. #include <stdio.h>   
  2. #include <time.h>   
  3. #include <dos.h>   
  4. #include <conio.h>   
  5. #include <windows.h>  
  6. int main()   
  7. {   
  8.     time_t t;   
  9.     time(&t);   
  10.     printf("Today's date and time: %s",ctime(&t));   
  11.   
  12.     time_t first, second;   
  13.     first=time(NULL);   
  14.     Sleep(2000);   
  15.     second=time(NULL);   
  16.     printf("The difference is: %f seconds",difftime(second,first));   
  17.     getch();   
  18.   
  19.     return 0;   
  20. }   
#include <stdio.h> 
#include <time.h> 
#include <dos.h> 
#include <conio.h> 
#include <windows.h>
int main() 
{ 
	time_t t; 
	time(&t); 
	printf("Today's date and time: %s",ctime(&t)); 

	time_t first, second; 
	first=time(NULL); 
	Sleep(2000); 
	second=time(NULL); 
	printf("The difference is: %f seconds",difftime(second,first)); 
	getch(); 

	return 0; 
} 

猜你喜欢

转载自blog.csdn.net/qq_26422355/article/details/80762282