c/c++在windows下获取时间和计算时间差的几种方法总结
一、标准C和C++都可用
1、获取时间用time_t time( time_t * timer ),计算时间差使用double difftime( time_t timer1, time_t timer0 )。 精确到秒。
测试程序如下:
- #include <time.h>
- #include <stdio.h>
- int main()
- {
- time_t start ,end ;
- double cost;
- time(&start);
- sleep(1);
- time(&end);
- cost=difftime(end,start);
- printf("%f/n",cost);
- return 0;
- }
本程序在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秒。
测试程序如下:
- #include <time.h>
- #include <stdio.h>
- int main()
- {
- double start,end,cost;
- start=clock();
- sleep(1);
- end=clock();
- cost=end-start;
- printf("%f/n",cost);
- return 0;
- }
二、C++中(此处针对windows环境,标准c中则linux和windows都可以)
1、GetTickCount()
调用函数需包含windows.h。得到的是系统运行的时间 精确到毫秒,测试程序如下:
- #include <iostream>
- #include <windows.h>
- using namespace std;
- int main()
- {
- double start = GetTickCount();
- Sleep(1000);
- double end=GetTickCount();
- cout << "GetTickCount:" << end-start << endl;
- return 0;
- }
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即可)。当然,精度肯定为秒了。
测试程序如下:
- #include <iostream>
- #include <windows.h>
- using namespace std;
- int main()
- {
- SYSTEMTIME start; //windows.h中
- GetLocalTime(&start);//time.h的tm结构体一样的效果
- cout<< start.year << endl;
- }
c语言的gmtime方法的示范代码如下:
- #include <time.h>
- #include <stdio.h>
- #include <stdlib.h>
- int main()
- {
- struct tm *tm_ptr;
- time_t the_time;
- (void) time(&the_time);
- tm_ptr = gmtime(&the_time);
- printf("Raw time is %ld/n", the_time);
- printf("gmtime gives:/n");
- printf("date: %02d/%02d/%02d/n",
- tm_ptr->tm_year, tm_ptr->tm_mon+1, tm_ptr->tm_mday);
- printf("time: %02d:%02d:%02d/n",
- tm_ptr->tm_hour, tm_ptr->tm_min, tm_ptr->tm_sec);
- exit(0);
- }
另外,c语言有类似于GetLocalTime方法的函数ctime()。
对localtime(),原型为:struct tm *localtime(const time_t *timep);将测试程序的gmtime改为localtime,则可以看到输出的时间为争取时间和日期了。为了更友好的得到时间和日期,像date那样输出,可以用asctime或ctime函数,原型:char *ctime(const time_t *timeval);测试代码如下:
- #include <time.h>
- #include <stdio.h>
- #include <stdlib.h>
- int main()
- {
- time_t the_time;
- time(&the_time);
- printf("The date is : %s /n" , ctime(&the_time));
- exit(0);
- }
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,不过经验证,可以不用包含)。测试代码如下:
- #include <iostream>
- #include <windows.h>//GetTickCount
- //#include <mmsystem.h>
- using namespace std;
- int main()
- {
- DWORD start = timeGetTime();//
- Sleep(1000);
- DWORD end= timeGetTime();//
- cout << timeGetTime() << endl;
- return 0;
- }
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下获取时间和计算时间差的几种方法总结相关推荐
- c语言windows获取时间,【转载】c/c++在windows下获取时间和计算时间差的几种方法总结...
一.标准C和C++都可用 1.获取时间用time_t time( time_t * timer ),计算时间差使用double difftime( time_t timer1, time_t time ...
- linux c计算时间差值,获取时间和计算时间差的几种方法总结,时间差几种方法...
获取时间和计算时间差的几种方法总结,时间差几种方法 一.标准C和C++都可用 1.获取时间用time_t time( time_t * timer ),计算时间差使用double difftime( ...
- bat中文乱码_详解Windows下获取时间bat脚本总结,值得收藏
概述 最近都在写ftp windows环境的一些bat脚本,为了方便显示,想要记录一下每次操作的时间,所以在这里总结一下windows环境bat脚本获取时间的常用命令. windows环境获取时间相关 ...
- Windows下获取视频设备的一种改进实现
之前在https://blog.csdn.net/fengbingchun/article/details/102806822中介绍过在Windows下获取视频设备列表的方法.其实那种实现方法是有缺陷 ...
- C++在windows下获取本地主机ipv4地址和ipv6地址的代码
把内容过程中经常用的内容段记录起来,下面内容段是关于C++在windows下获取本地主机ipv4地址和ipv6地址的内容,应该是对码农有所用处. #include <Winsock2.h> ...
- Windows下获取usb视频设备vendor id和product id的4种方法
之前在https://blog.csdn.net/fengbingchun/article/details/103507754 博文中介绍过如何在Linux获取usb视频设备的vendor id和pr ...
- Linux :: 时间日历指令【1】:date 指令:格式化显示时间信息、Linux 下获取时间
前言:本篇是 Linux 基本操作篇章的内容! 笔者使用的环境是基于腾讯云服务器:CentOS 7.6 64bit. 学习集: C++ 入门到入土!!!学习合集 Linux 从命令到网络再到内核!学习 ...
- php sendmail方法,PHP实现在windows下配置sendmail并通过mail()函数发送邮件的方法
本文实例讲述了PHP实现在windows下配置sendmail并通过mail()函数发送邮件的方法.分享给大家供大家参考,具体如下: 1.php mail()函数在windows不能用,需要安装sen ...
- Windows下安装Redis及使用Python操作Redis的方法
这篇文章主要介绍了Windows下安装Redis及使用Python操作Redis的方法,非常不错,具有参考借鉴价值,需要的朋友可以参考下 首先说一下在Windows下安装Redis,安装包可以在htt ...
最新文章
- redis 数据结构 内存管理 持久化
- 【java网络编程】用TCP socket实现多线程图片上传
- mysql主从克隆服务器_mysql主从复制服务器配置
- Arctext.js - 基于 CSS3 jQuery 的文本弯曲效果
- nextcloud+nginx+mysql,Ubuntu搭建Nginx-Nextcloud环境
- 惯用过程模型_惯用的Ruby:编写漂亮的代码
- 联想一体机电源键不亮_联想电脑一体机B505拆机经验
- [UWP]了解模板化控件(4):TemplatePart
- Linux socket can例程C++版本
- SVN(TortoiseSVN)提交时忽略bin跟obj目录
- 数据结构与算法之单链表(1)
- 误删除了Oracle的dbf文件后的解决方法
- Python学习之路day3-文件操作
- 应用密码学:协议、算法与C源程序(学习第一章)
- 第三方登录之QQ登录(一)——QQ互联开放平台新建应用
- Vue 安装@vue/cli报错npmERR gyp ERR
- PHP中fileinfo的作用以及怎么开启fileinfo
- 已经过去2周了,你感觉怎么样?
- 我说CMMI之五:CMMI 4个等级的区别
- 机器视觉工业相机选型
热门文章
- 数组的合并和升序排列_JavaScript - 数组排序 6 种常见算法是什么?
- python类方法继承_对python中类的继承与方法重写介绍
- android模拟器 后退键,MainActivity返回键模拟home效果,容易出现的问题
- java 自定义http头_HttpClient自定义HTTP头
- 新时达二代操作器刷写数据线_布袋除尘器的脉冲振动特点及安装操作
- 2019-11-18 自动控制原理的几个基本概念
- select自定义箭头问题 。。。和一行内不同颜色的整体鼠标滑过变色
- 删除已有的 HTML 元素
- Ubuntu11.10配置Eclipse下Android开发环境
- .NET 2.0 RC的一个Bug