http://blog.csdn.net/coder_xia/article/details/6566708

一、标准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. int main()
  4. {
  5. time_t start ,end ;
  6. double cost;
  7. time(&start);
  8. sleep(1);
  9. time(&end);
  10. cost=difftime(end,start);
  11. printf("%f/n",cost);
  12. return 0;
  13. }

本程序在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. int main()
  4. {
  5. double start,end,cost;
  6. start=clock();
  7. sleep(1);
  8. end=clock();
  9. cost=end-start;
  10. printf("%f/n",cost);
  11. return 0;
  12. }

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

1、GetTickCount()

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

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

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. using namespace std;
  4. int main()
  5. {
  6. SYSTEMTIME start; //windows.h中
  7. GetLocalTime(&start);//time.h的tm结构体一样的效果
  8. cout<< start.year << endl;
  9. }

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

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

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

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

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

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

测试程序如下:

  1. #include <iostream>
  2. #include <windows.h>
  3. using namespace std;
  4. int main()
  5. {
  6. LARGE_INTEGER m_nFreq;
  7. LARGE_INTEGER m_nBeginTime;
  8. LARGE_INTEGER nEndTime;
  9. QueryPerformanceFrequency(&m_nFreq); // 获取时钟周期
  10. QueryPerformanceCounter(&m_nBeginTime); // 获取时钟计数
  11. Sleep(100);
  12. QueryPerformanceCounter(&nEndTime);
  13. cout << (double)(nEndTime.QuadPart-m_nBeginTime.QuadPart)*1000/m_nFreq.QuadPart << endl;
  14. }

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

4、timeGetTime()。

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

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

5、MFC中,CTime::GetCurrentTime() 精确到秒,不列出测试代码。

关于定时器什么的,目前用到地方不多,就不总结了

参考网址:

1、http://blog.csdn.net/wallaceli1981/archive/2009/10/24/4723218.aspx

2、http://wenku.baidu.com/view/beb3c9eef8c75fbfc77db2b5.html

c/c++在windows下获取时间和计算时间差的几种方法总结 【转】的更多相关文章

  1. 【转载】c/c++在windows下获取时间和计算时间差的几种方法总结

    一.标准C和C++都可用 1.获取时间用time_t time( time_t * timer ),计算时间差使用double difftime( time_t timer1, time_t time ...

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

    http://blog.csdn.net/caimagic/article/details/50696609 我用的是GetTickCount(), 获取到的是毫秒.

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

    c/c++在windows下获取时间和计算时间差的几种方法总结 一.标准C和C++都可用 1.获取时间用time_t time( time_t * timer ),计算时间差使用double diff ...

  4. Windows下Apache+MySQL+PHP快速配置的几种方法

    Apache MySQL PHP Windows WAMP 1.易思EasySiteServer服务器集成环境 v1.0  (推荐) 尔创互联为推广其ESPCMS而开发的一个小东东,很好用.零配置,完 ...

  5. Windows下获取高精度时间注意事项

    Windows下获取高精度时间注意事项 [转贴 AdamWu]   花了很长时间才得到的经验,与大家分享. 1. RDTSC - 粒度: 纳秒级 不推荐优势: 几乎是能够获得最细粒度的计数器抛弃理由: ...

  6. windows下获取IP地址的两种方法

    windows下获取IP地址的两种方法: 一种可以获取IPv4和IPv6,但是需要WSAStartup: 一种只能取到IPv4,但是不需要WSAStartup: 如下: 方法一:(可以获取IPv4和I ...

  7. Windows下获取本机IP地址方法介绍

    Windows下获取本机IP地址方法介绍 if((hostinfo = gethostbyname(name)) != NULL) { #if 1 ; printf("IP COUNT: % ...

  8. C语言实现Windows下获取IP和MAC地址。

    C语言实现Windows下获取IP和MAC地址. #include <winsock2.h> #include <stdio.h> #include <stdlib.h& ...

  9. .pages怎么在windows上打开?Windows下打开在Mac中编辑的.pages文件方法

    .pages怎么在windows上打开?Windows下打开在Mac中编辑的.pages文件方法 1.最简单的方法是修改后缀名为.zip然后解压,解压后就可以看到一张图片,这个就是文档内容了. 2.更 ...

随机推荐

  1. LeetCode解题报告—— Maximal Rectangle

    Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and ...

  2. centos安装更新Python2.7以及pip的安装

    一.首先对相关的软件进行更新 python -V yum -y update yum groupinstall -y development yum install -y zlib zlib-dev ...

  3. git: Your branch and 'origin/master' have diverged

    git: Your branch and 'origin/master' have diverged - how to throw away local commits? - Stack Overfl ...

  4. 如何简单的测试kubernetes的dns add-ons是否工作正常?

    1,新建一个yaml文件. apiVersion: v1 kind: Pod metadata: name: busybox namespace: default spec: containers: ...

  5. 事务管理配置与@Transactional注解使用

    spring,mybatis事务管理配置与@Transactional注解使用 概述 事务管理对于企业应用来说是至关重要的,即使出现异常情况,它也可以保证数据的一致性. Spring Framewor ...

  6. schtasks命令

    1.创建任务 在每天的22.44定时执行一次. schtasks /create /tn : 在特定时间运行一次. schtasks /create /tn : /sd // 2.运行一次任务 创建任 ...

  7. CodeForces 144B Meeting

    暴力. 题目只要求计算边上的点就可以了,一开始没看清题意,把内部的也算进去了.内部的计算可以延迟标记一下,但这题没有必要. #include<map> #include<set> ...

  8. 【java回调】同步/异步回调机制的原理和使用方法

    回调(callback)在我们做工程过程中经常会使用到,今天想整理一下回调的原理和使用方法. 回调的原理可以简单理解为:A发送消息给B,B处理完后告诉A处理结果.再简单点就是A调用B,B调用A. 那么 ...

  9. Xamarin.Forms获取设备屏幕大小

    Xamarin.Forms获取设备屏幕大小 可以借助device.Display获取.基本形式如下: var display = device.Display;然后就可以获取屏幕大小.display. ...

  10. 【点分治】【FFT】Gym - 101234D - Forest Game

    存个求树上每种长度(长度定义为路径上点数)的路径条数的模板:num数组中除了长度为1的以外,都算了2次. 不造为啥FFT数组要开八倍. #include<cstdio> #include& ...