c/c++在 windows 下获取时间和计算时间差的几种方法
总结
初级经济法重点总结下载党员个人总结TXt高中句型全总结.doc高中句型全总结.doc理论力学知识点总结pdf
一、
标准
excel标准偏差excel标准偏差函数exl标准差函数国标检验抽样标准表免费下载红头文件格式标准下载
C 和 C++都可用
1、获取时间用 time_t time( time_t * timer ),计算时间差使用 double
difftime( time_t timer1, time_t timer0 )。 精确到秒。
测试程序如下:
1. #include
2. #include
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
函数
excel方差函数excelsd函数已知函数 2 f x m x mx m 2 1 4 2拉格朗日函数pdf函数公式下载
,需要注意的是:
1)在 windows 下,为 Sleep()函数,且需包含 windows.h
2)关于 sleep 中的数,在 Windows 和 Linux 下 1000 代
表
关于同志近三年现实表现材料材料类招标技术评分表图表与交易pdf视力表打印pdf用图表说话 pdf
的含义并不相同,Windows
下的表示 1000 毫秒,也就是 1 秒钟;Linux 下表示 1000 秒,Linux 下使用毫秒级别的
函数可以使用 usleep。
2、clock_t clock()
clock() 获取的是计算机启动后的时间间隔,得到的是 CPU 时间,精确到
1/CLOCKS_PER_SEC 秒。
测试程序如下:
1. #include
2. #include
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
2. #include
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
2. #include
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
2. #include
3. #include
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
2. #include
3. #include
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
2. #include
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.Q
uadPart << endl;
14. }
需要注意的就是结果需要强制转换为 double,不然会得到如下错误:<< is ambiguous。
4、timeGetTime()。
精度:毫秒,与 GetTickCount()相当。使用需要包含 windows.h,并加入 Winmm.lib
(虽然查到
资料
新概念英语资料下载李居明饿命改运学pdf成本会计期末资料社会工作导论资料工程结算所需资料清单
说需要包含 mmsystem.h,不过经验证,可以不用包含)。
测试程序如下:
1. #include
2. #include
3. //#include
4. using namespace std;
5. int main()
6. {
7. DWORD start = timeGetTime();
8. Sleep(1000);
9. DWORD end= timeGetTime();
10. cout << end-start << endl;
11. return 0;
12. }
5、MFC 中,CTime::GetCurrentTime() 精确到秒,不列出测试代码。
关于定时器什么的,目前用到地方不多,就不总结了
参考网址:
1、
http://blog.csdn.net/wallaceli1981/archive/2009/10/24/4723218.a
spx
2、http://wenku.baidu.com/view/beb3c9eef8c75fbfc77db2b5.html
博客地址:
http://blog.csdn.net/coder_xia/archive/2011/06/24/6566708.aspx