c/c++在windows下获取时间和计算时间差的几种方法总结
2013-09-04 18:01
756 查看
c/c++在windows下获取时间和计算时间差的几种方法总结
分类:c/c++进行时 2011-06-24 22:47
6563人阅读
评论(6)
收藏
举报
windowsinteger测试timerstructlinux
一、标准C和C++都可用
1、获取时间用time_t time( time_t * timer ),计算时间差使用double difftime( time_t timer1, time_t timer0 )。 精确到秒。
测试程序如下:
[c-sharp]
view plaincopyprint?
#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;
}
2、clock_t clock(),clock() 获取的是计算机启动后的时间间隔,得到的是CPU时间,精确到1/CLOCKS_PER_SEC秒。 测试程序如下: [c-sharp] view plaincopyprint? #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; } #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。得到的是系统运行的时间 精确到毫秒,测试程序如下:
[c-sharp]
view plaincopyprint?
#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;
}
[c-sharp] view plaincopyprint? #include <iostream> #include <windows.h> using namespace std; int main() { SYSTEMTIME start; //windows.h中 GetLocalTime(&start);//time.h的tm结构体一样的效果 cout<< start.year << endl; } #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方法的示范代码如下:
[c-sharp]
view plaincopyprint?
#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-sharp] view plaincopyprint? #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); } #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就得到时间。
测试程序如下:
[c-sharp]
view plaincopyprint?
#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;
}
[c-sharp] view plaincopyprint? #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; } #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++编程思想151页遇到的内存分配问题
下一篇:C/C++语言中的void及void指针总结
查看评论
3楼 ninanangel
2013-07-22 11:57发表
[回复] [引用]
[举报]
谢谢,很有用
Re: coder_xia
2013-07-22 23:40发表
[回复] [引用]
[举报]
回复ninanangel:有用就好
2楼 刺目的风
2013-04-21 18:10发表
[回复] [引用]
[举报]
有用
Re: coder_xia
2013-05-04 08:32发表
[回复] [引用]
[举报]
回复u010279421:有用就好
1楼 shanhe1986
2012-12-12 14:33发表
[回复] [引用]
[举报]
原创顶一个
Re: coder_xia
2013-05-04 08:31发表
[回复] [引用]
[举报]
回复shanhe1986:谢了,其实是参考别人的自己再总结了一下,用代码都试了下而已
相关文章推荐
- c/c++在windows下获取时间和计算时间差的几种方法总结
- c/c++在windows下获取时间和计算时间差的几种方法总结 【转】
- c++ 在windows下获取时间和计算时间差的几种方法总结
- 【转载】c/c++在windows下获取时间和计算时间差的几种方法总结
- c/c++在windows下获取时间和计算时间差的几种方法总结
- c/c++在windows下获取时间和计算时间差的几种方法总结
- c++ 在windows下获取时间和计算时间差的几种方法总结
- c/c++在windows下获取时间和计算时间差的几种方法总结
- c/c++在windows下获取时间和计算时间差的几种方法总结
- c/c++在windows下获取时间和计算时间差的几种方法总结
- c/c++在windows下获取时间和计算时间差的几种方法总结
- c++ 在windows下获取时间和计算时间差的几种方法总结
- c/c++在windows下获取时间和计算时间差的几种方法总结
- c/c++在windows下获取时间和计算时间差的几种方法总结
- c/c++获取时间和计算时间差的几种方法总结
- windows下获取时间和计算时间差的几种方法总结
- c和c++在windows下获取时间和计算时间差的方法总结
- [转]C++获取Windows时间的方法总结
- 4000 获取时间和计算时间差的几种方法总结
- C++获取Windows时间方法的总结