Windows和Linux下计时函数总结

本文对Windows及Linux平台下常用的计时函数进行总结,包括精度为秒、毫秒、微秒三种精度的各种函数。
比如Window平台下特有的Windows API函数GetTickCount()、timeGetTime()、及QueryPerformanceCounter(),
Linux平台下特有的gettimeofday()函数,以及标准的C/C++函数time()和clock()。下面分别对此进行简单介绍并附上示例代码。

通用的C/C++计时函数time()和clock()

time_t time(time_t *timer);
返回以格林尼治时间(GMT)为标准,从1970年1月1日00:00:00到现在的此时此刻所经过的秒数。
time_t实际是个long长整型typedef long time_t;

clock_t clock(void);
返回进程启动到调用函数时所经过的CPU时钟计时单元(clock tick)数,在MSDN中称之为挂钟时间(wal-clock),以毫秒为单位。
clock_t实际是个long长整型typedef long clock_t;

Window平台特有函数
DWORD timeGetTime(void);
返回系统时间,以毫秒为单位。系统时间是从系统启动到调用函数时所经过的毫秒数。注意,这个值是32位的,会在0到2^32之间循环,约49.71天。

DWORD WINAPI GetTickCount(void);
这个函数和timeGetTime()一样也是返回系统时间,以毫秒为单位。

高精度计时,以微秒为单位(1毫秒=1000微秒)。
BOOL QueryPerformanceCounter(LARGE_INTEGER *lpPerformanceCount);得到高精度计时器的值(如果存在这样的计时器)。
BOOL QueryPerformanceFrequency(LARGE_INTEGER *lpFrequency);返回硬件支持的高精度计数器的频率(次每秒),返回0表示失败。
其中LARGE_INTEGER其实是一个联合体,可以得到__int64 QuadPart;也可以分别得到低32位DWORD LowPart和高32位的值LONG HighPart。
在使用时,先使用QueryPerformanceFrequency()得到计数器的频率,再计算二次调用QueryPerformanceCounter()所得的计时器值之差,
用差去除以频率就得到精确的计时了

Linux平台特有函数
int gettimeofday(struct timeval *tv,struct timezone *tz);
获得当前精确时间(1970年1月1日到现在的时间),精度为微秒。
保存时间的结构体
strut timeval {
long tv_sec; //秒数
long tv_usec; //微秒数
};

#include <iostream>

#if defined(_WIN32) || defined(WIN32)        /**Windows*/
#define WINDOWS_IMPL
#include <windows.h>
#include <time.h>            //time() 、 clock()
#include <Mmsystem.h>       //timeGetTime()
#pragma comment(lib, "Winmm.lib") //timeGetTime()
#elif defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__) || defined(BSD)    /**Linux*/
#define LINUX_IMPL
#include <sys/time.h>        //gettimeofday()
#endif
#include <stdio.h>

/***********************************************************
通用的:
time_t time(time_t *tloc);     //返回从1970年1月1日0点以来的秒数,精度为秒
clock_t clock(): 返回该程序从启动到函数调用占用CPU的时间,精度为毫秒,但一般最小精度是33ms

Windows特有:
GetTickCount(): 返回从操作系统启动到现在所经过的毫秒数,精度毫秒,但最小精度是18ms
                返回值以32位的双字类型DWORD存储,因此可以存储的最大值是2^32 ms约为49.71天,
timeGetTime():    返回以毫秒计的系统时间,该时间为从系统开启算起所经过的时间,精度为毫秒
QueryPerformanceCounter(): 返回高精确度性能计数器的值,精度为微妙,但是确切的精确计时的最小单位是与系统有关的

Linux特有:
gettimeofday(struct timeval *tv,struct timezone *tz); 获得当前精确时间(1970年1月1日到现在的时间),精度为微秒
***********************************************************/

void MySleep(int sec_time)
{
    #if defined(WINDOWS_IMPL)
        Sleep(sec_time*1000);
    #elif defined(LINUX_IMPL)
        sleep(sec_time);
    #endif
}

void test_time()
{
    //通用的
    //用time()来计时  秒
    time_t timeBegin, timeEnd;
    timeBegin = time(NULL);
    MySleep(1);
    timeEnd = time(NULL);
    printf("%d\n", timeEnd - timeBegin);

    /*
     * Structure used in select() call, taken from the BSD file sys/time.h.
     */
    //struct timeval {
    //        long    tv_sec;         /* seconds */
    //        long    tv_usec;        /* and microseconds */
    //};
    timeval  val;

    //用clock()来计时  毫秒
    clock_t  clockBegin, clockEnd;
    clockBegin = clock();
    MySleep(1);
    clockEnd = clock();
    printf("%d\n", clockEnd - clockBegin);

#ifdef WINDOWS_IMPL
    //Windows

    //用GetTickCount()来计时  毫秒
    DWORD  dwGTCBegin, dwGTCEnd;
    dwGTCBegin = GetTickCount();
    Sleep(1000);
    dwGTCEnd = GetTickCount();
    printf("%d\n", dwGTCEnd - dwGTCBegin);

    //用timeGetTime()来计时  毫秒
    DWORD  dwBegin, dwEnd;
    dwBegin = timeGetTime();
    Sleep(1000);
    dwEnd = timeGetTime();
    printf("%d\n", dwEnd - dwBegin);

    //用QueryPerformanceCounter()来计时  微秒
    LARGE_INTEGER  large_interger;
    double dff;
    __int64  c1, c2;
    QueryPerformanceFrequency(&large_interger);
    dff = large_interger.QuadPart;
    QueryPerformanceCounter(&large_interger);
    c1 = large_interger.QuadPart;
    Sleep(1000);
    QueryPerformanceCounter(&large_interger);
    c2 = large_interger.QuadPart;
    printf("高精度计时器频率%lf\n", dff);
    printf("第一次计时器值%I64d 第二次计时器值%I64d 计时器差%I64d\n", c1, c2, c2 - c1);
    printf("计时%lf毫秒\n", (c2 - c1) * 1000 / dff);

#elif  defined(LINUX_IMPL)
    //Linux

    struct timeval tpstart,tpend;
    double timeuse;
    gettimeofday(&tpstart,NULL);
    sleep(1);
    gettimeofday(&tpend,NULL);
    timeuse=1000000*(tpend.tv_sec-tpstart.tv_sec)+tpend.tv_usec-tpstart.tv_usec;//注意,秒的读数和微秒的读数都应计算在内
    printf("used time:%fus\n",timeuse);
#endif

}

int main()
{
    test_time();
        getchar();
    return 0;
}

转载地址:http://www.cnblogs.com/lizhenghn/p/3587799.html

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值