c/c++在windows下获取时间和计算时间差的几种方法总结 【转】
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 )。 精确到秒。
测试程序如下:
- #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
c/c++在windows下获取时间和计算时间差的几种方法总结 【转】的更多相关文章
- 【转载】c/c++在windows下获取时间和计算时间差的几种方法总结
一.标准C和C++都可用 1.获取时间用time_t time( time_t * timer ),计算时间差使用double difftime( time_t timer1, time_t time ...
- c++ 在windows下获取时间和计算时间差的几种方法总结
http://blog.csdn.net/caimagic/article/details/50696609 我用的是GetTickCount(), 获取到的是毫秒.
- c和c++在windows下获取时间和计算时间差的方法总结
c/c++在windows下获取时间和计算时间差的几种方法总结 一.标准C和C++都可用 1.获取时间用time_t time( time_t * timer ),计算时间差使用double diff ...
- Windows下Apache+MySQL+PHP快速配置的几种方法
Apache MySQL PHP Windows WAMP 1.易思EasySiteServer服务器集成环境 v1.0 (推荐) 尔创互联为推广其ESPCMS而开发的一个小东东,很好用.零配置,完 ...
- Windows下获取高精度时间注意事项
Windows下获取高精度时间注意事项 [转贴 AdamWu] 花了很长时间才得到的经验,与大家分享. 1. RDTSC - 粒度: 纳秒级 不推荐优势: 几乎是能够获得最细粒度的计数器抛弃理由: ...
- windows下获取IP地址的两种方法
windows下获取IP地址的两种方法: 一种可以获取IPv4和IPv6,但是需要WSAStartup: 一种只能取到IPv4,但是不需要WSAStartup: 如下: 方法一:(可以获取IPv4和I ...
- Windows下获取本机IP地址方法介绍
Windows下获取本机IP地址方法介绍 if((hostinfo = gethostbyname(name)) != NULL) { #if 1 ; printf("IP COUNT: % ...
- C语言实现Windows下获取IP和MAC地址。
C语言实现Windows下获取IP和MAC地址. #include <winsock2.h> #include <stdio.h> #include <stdlib.h& ...
- .pages怎么在windows上打开?Windows下打开在Mac中编辑的.pages文件方法
.pages怎么在windows上打开?Windows下打开在Mac中编辑的.pages文件方法 1.最简单的方法是修改后缀名为.zip然后解压,解压后就可以看到一张图片,这个就是文档内容了. 2.更 ...
随机推荐
- ReentrantLock 学习
Java接口Lock有三个实现类:ReentrantLock.ReentrantReadWriteLock.ReadLock和ReentrantReadWriteLock.WriteLock.Lock ...
- Search in Rotated Sorted Array I&&II——二分法
Search in Rotated Sorted Array I Suppose a sorted array is rotated at some pivot unknown to you befo ...
- Android与html5交互 -- WebView使用(一)
Android中使用WebView可加载html5,具体步骤如下: (前提:本地Html5存放到assets文件夹下) 一:使用WebView加载Html5,简单显示 1:清单文件中添加访问权限:an ...
- Django CRM查询 XXX.object.filter() 常用用法总结
__gt 大于 __gte 大于等于 User.objects.filter(age__gt=10) // 查询年龄大于10岁的用户 User.objects.filter(age__gte=10) ...
- 19. Remove Nth Node From End of List【Medium】【删除单链表倒数第n个结点】
Given a linked list, remove the n-th node from the end of list and return its head. Example: Given l ...
- 链式前向星实现的堆优化dij求最短路模板
#include<cstdio> #include<string> #include<cstdlib> #include<cmath> #include ...
- 洛谷P3901 数列找不同 [莫队]
题目传送门 题目描述 现有数列 A_1,A_2,\cdots,A_NA1,A2,⋯,AN ,Q 个询问 (L_i,R_i)(Li,Ri) , A_{Li} ,A_{Li+1},\cdots, ...
- 制作启动U盘
概述 将普通的u盘制作成启动u盘,用于引导安装操作系统. 材料: 普通U盘 需要有足够的存储空间,里面的内容请提前备份. 操作系统iso文件 PowerISO 商业软件,有试用期:用来制作启动u盘 正 ...
- go chapter 7 - 类型
任意类型 interface{} 遍历并判断类型 func MyPrintf(args ...interface{}) { for _, arg := range args { switch arg. ...
- Codeforces Round #277 (Div. 2) D. Valid Sets (DP DFS 思维)
D. Valid Sets time limit per test 1 second memory limit per test 256 megabytes input standard input ...