c和c++在windows下获取时间和计算时间差的方法总结

c/c++在windows下获取时间和计算时间差的几种方法总结

一、标准C和C++都可用

1、获取时间用time_t time( time_t * timer ),计算时间差使用double

difftime( time_t timer1, time_t timer0 )。 精确到秒。

测试程序如下: 

 1 #include <time.h>
 2 #include <stdio.h>
 3 
 4 int main()
 5 {
 6     time_t start, end;
 7     double cost;
 8     
 9     time(&start);
10     sleep(1);
11     time(&end);
12     cost=difftime(end,start);
13     printf("%f
",cost);
14     
15     return 0;
16 }

本程序在fedora9测试通过。

关于代码中的sleep函数,需要注意的是:

1)在windows下,为Sleep()函数,且需包含windows.h

2)关于sleep中的数,在Windows和Linux下1000代表的含义并不相同,Windows下的表示1000毫秒,也就是1秒钟;Linux下表示1000秒,Linux下使用毫秒级别的函数可以使用usleep。

2、clock_t clock()

clock() 获取的是计算机启动后的时间间隔,得到的是CPU时间,精确到

1/CLOCKS_PER_SEC秒。

测试程序如下:

 1 #include <time.h>
 2 #include <stdio.h>
 3 
 4 int main()
 5 {
 6     double start, end, cost;
 7     
 8     start=clock();
 9     sleep(1);
10     end=clock();
11     cost=end-start;
12     printf("%f
",cost);
13     
14     return 0;
15 }

 

二、C++中(此处针对windows环境,标准c中则linux和windows都可以)

1、GetTickCount()

调用函数需包含windows.h。得到的是系统运行的时间 精确到毫秒,测试程序如下:

 1 #include <iostream>
 2 #include <windows.h>
 3 
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     double start = GetTickCount();
 9     Sleep(1000);
10     double end=GetTickCount();
11     cout << "GetTickCount:" << end-start << endl;
12     
13     return 0;
14 }

2、GetLocalTime()

获得的是结构体保存的year,month等信息。而C语言time函数获得是从1970年1月1日0时0分0秒到此时的秒数。需要gmtime函数转换为常用的日历(返回的是世界时间,要显示常用的时间,则为localtime函数)。

在c语言中,保存常用日历的结构体为struct tm,包含在time.h中,c++语言为SYSTEMTIME结构体,包含在winbase.h(编程包含windows.h即可)。当然,精度肯定为秒了。

测试程序如下:

 1 #include <iostream>
 2 #include <windows.h>
 3 
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     SYSTEMTIME start; //windows.h中
 9     GetLocalTime(&start);//time.h的tm结构体一样的效果
10     cout<< start.year << endl;
11 }

c语言的gmtime方法的示范代码如下:

 1 #include <time.h>
 2 #include <stdio.h>
 3 #include <stdlib.h>
 4 
 5 int main()
 6 {
 7     struct tm *tm_ptr;
 8     time_t the_time;
 9     (void) time(&the_time);
10     
11     tm_ptr = gmtime(&the_time);
12     printf("Raw time is %ld
", the_time);
13     printf("gmtime gives:
");
14     printf("date: %02d/%02d/%02d
", tm_ptr->tm_year, tm_ptr->tm_mon+1, tm_ptr->tm_mday);
15     printf("time: %02d:%02d:%02d
", tm_ptr->tm_hour, tm_ptr->tm_min, tm_ptr->tm_sec);
16     exit(0);
17 }

另外,c语言类似于GetLocalTime方法的函数为ctime()。

对localtime(),原型为:struct tm *localtime(const time_t *timep);将测试程序的gmtime改为localtime,则可以看到输出的时间为争取时间和日期了。为了更友好的得到时间和日期,像date那样输出,可以用asctime或ctime函数,原型:char *ctime(const time_t *timeval);

测试程序如下:

 1 #include <time.h>
 2 #include <stdio.h>
 3 #include <stdlib.h>
 4 
 5 int main()
 6 {
 7     time_t the_time;
 8     time(&the_time);
 9     printf("The date is : %s 
" , ctime(&the_time));
10     
11     exit(0);
12 }

3、要获取高精度时间,可以使用

BOOL QueryPerformanceFrequency(LARGE_INTEGER *lpFrequency)获取系统的计数器的频率

BOOL QueryPerformanceCounter(LARGE_INTEGER *lpPerformanceCount)获取计数器的值

然后用两次计数器的差除以Frequency就得到时间。

测试程序如下:

#include <iostream>
#include <windows.h>
using namespace std;

int main()
{
    LARGE_INTEGER m_nFreq;
    LARGE_INTEGER m_nBeginTime;
    LARGE_INTEGER nEndTime;
    
    QueryPerformanceFrequency(&m_nFreq); // 获取时钟周期
    QueryPerformanceCounter(&m_nBeginTime); // 获取时钟计数
    Sleep(100);
    QueryPerformanceCounter(&nEndTime);
    cout << (double)(nEndTime.QuadPart-m_nBeginTime.QuadPart)*1000/m_nFreq.QuadPart << endl;
}

需要注意的就是结果需要强制转换为double,不然会得到如下错误:<< is ambiguous。

4、timeGetTime()。

精度:毫秒,与GetTickCount()相当。使用需要包含windows.h,并加入Winmm.lib(虽然查到资料说需要包含mmsystem.h,不过经验证,可以不用包含)。

测试程序如下:

 1 #include <iostream>
 2 #include <windows.h>
 3 //#include <mmsystem.h>
 4 
 5 using namespace std;
 6 
 7 int main()
 8 {
 9     DWORD start = timeGetTime();
10     Sleep(1000);
11     DWORD end= timeGetTime();
12     cout << end-start << endl;
13     
14     return 0;
15 }
原文地址:https://www.cnblogs.com/hushaojun/p/5639780.html