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();
time(&end);
cost=difftime(end,start);
printf("%f\n",cost); return ;
}

本程序在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();
end=clock();
cost=end-start;
printf("%f\n",cost); return ;
}

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

1、GetTickCount()

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

 #include <iostream>
#include <windows.h> using namespace std; int main()
{
double start = GetTickCount();
Sleep();
double end=GetTickCount();
cout << "GetTickCount:" << end-start << endl; return ;
}

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+, tm_ptr->tm_mday);
printf("time: %02d:%02d:%02d\n", tm_ptr->tm_hour, tm_ptr->tm_min, tm_ptr->tm_sec);
exit();
}

另外,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();
}

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();
QueryPerformanceCounter(&nEndTime);
cout << (double)(nEndTime.QuadPart-m_nBeginTime.QuadPart)*/m_nFreq.QuadPart << endl;
}

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

4、timeGetTime()。

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

测试程序如下:

 #include <iostream>
#include <windows.h>
//#include <mmsystem.h> using namespace std; int main()
{
DWORD start = timeGetTime();
Sleep();
DWORD end= timeGetTime();
cout << end-start << endl; return ;
}

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/c++在windows下获取时间和计算时间差的几种方法总结 【转】

    http://blog.csdn.net/coder_xia/article/details/6566708 一.标准C和C++都可用 1.获取时间用time_t time( time_t * tim ...

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

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

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

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

  5. Windows下获取文件的md5码的方法

    1.certutil 命令简介 本来想找一个工具 算一个文件的md5 或者是sha 值来着. 找到一个说法是 可以使用 windows 自带的命令行来处理 具体命令 certutil -hashfil ...

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

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

  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. 排错-windows下 ORA-12560 TNS 协议适配器错误解决方法

    排错-windows下_ORA-12560 TNS 协议适配器错误解决方法 by:授客 QQ:1033553122 问题描述: 修改SQL*Plus窗口属性后,重新打开SQL*Plus时出现ORA-1 ...

随机推荐

  1. python进程理论部分

    一 什么是进程 进程:正在进行的一个过程或者说一个任务.而负责执行任务则是cpu. 举例(单核+多道,实现多个进程的并发执行): sxx在一个时间段内有很多任务要做:python备课的任务,写书的任务 ...

  2. django 分页django-pure-pagination(zz)

    虽然django自带了一个paginator,但不是很方便,我们使用django-pure-pagination github地址https://github.com/jamespacileo/dja ...

  3. qt include_directories没用

    include_directories之后在.cpp文件中include.h文件没有路径补全说明qt没有找到该.h文件 问题解决: 在add_executable里加入那个.cpp文件就可以了 因为c ...

  4. MATLAB的cftool工具箱简介

    下面,通过一个例子说明cftool可视化界面工具箱的用法. 例如,已知 x = [0 0.2 0.50.8 0.9 1.3 1.4 1.9 2.1 2.2 2.5 2.6 2.9 3.0]; y = ...

  5. CF 1006C Three Parts of the Array【双指针/前缀和/后缀和/二分】

    You are given an array d1,d2,-,dn consisting of n integer numbers. Your task is to split this array ...

  6. (转) C#解惑:HashSet<T>类

    HashSet<T>是一个相对“冷门”的类型,平时在项目中用得不多,但是在特定的业务中可以大用. 先来了解下HashSet<T>类,主要被设计用来存储集合,做高性能集运算,例如 ...

  7. Hibernate所有缓存机制详解

    hibernate提供的一级缓存 hibernate是一个线程对应一个session,一个线程可以看成一个用户.也就是说session级缓存(一级缓存)只能给一个线程用,别的线程用不了,一级缓存就是和 ...

  8. Eclipse导入idea 项目

    学校做的项目老师会导入到Eclipse下查看. 使用idea做的项目直接导入eclipse会发生问题(file-Export to Eclipse) 让Eclipse兼容idea项目,或者直接导入id ...

  9. POJ 1127 Jack Straws (计算几何)

    [题目链接] http://poj.org/problem?id=1127 [题目大意] 在二维平面中,给出一些木棍的左右端点,当木棍相交或者间接相交时 我们判断其连通,给出一些询问,问某两个木棍是否 ...

  10. Problem I: 零起点学算法30——输出四位完全平方数

    #include<stdio.h> int main() { int a,b,c,d,s,i; ;i<;i++){ s=i*i; a=s/; b=s%/; c=s%/; d=s%; ...