• 计算时间差代码


    View Code
     1 #include <iostream>
    2 #include <ctime>
    3
    4
    5
    6 using namespace std;
    7
    8 int main(int argv, char * agrc)
    9 {
    10
    11 struct tm currentTime, beforeTime;
    12 long diff;
    13 time_t now,before;
    14 beforeTime.tm_year=2011-1900;
    15 beforeTime.tm_mon=1-1;
    16 beforeTime.tm_mday=30;
    17 beforeTime.tm_hour=23;
    18 beforeTime.tm_min=59;
    19 beforeTime.tm_sec=55;
    20 beforeTime.tm_isdst=-1;
    21 before=mktime(&beforeTime);
    22 currentTime.tm_year = 2011-1900;
    23 currentTime.tm_mon = 2-1;
    24 currentTime.tm_mday = 1;
    25 currentTime.tm_hour = 0;
    26 currentTime.tm_min = 0;
    27 currentTime.tm_sec = 4;
    28 now = mktime(&currentTime);
    29 diff = difftime(now,before);
    30 cout << diff << endl;
    31 return 0;
    32 }

     本文对Windows平台下常用的计时函数进行总结,包括精度为秒、毫秒、微秒三种精度的5种方法。分为在标准C/C++下的二种time()及clock(),标准C/C++所以使用的time()及clock()不仅可以用在Windows系统,也可以用于Linux系统。在Windows系统下三种,使用Windows提供的API接口timeGetTime()、GetTickCount()及QueryPerformanceCounter()来完成。文章最后给出了5种计时方法示例代码。

     

    标准C/C++的二个计时函数time()及clock()

     

    time_t time(time_t *timer);

    返回以格林尼治时间(GMT)为标准,从1970年1月1日00:00:00到现在的此时此刻所经过的秒数。

    time_t实际是个long长整型typedef long time_t;

    头文件:#include <time.h>

     

    clock_t clock(void);

    返回进程启动到调用函数时所经过的CPU时钟计时单元(clock tick)数,在MSDN中称之为挂钟时间(wal-clock),以毫秒为单位。

    clock_t实际是个long长整型typedef long clock_t;

    头文件:#include <time.h>

     

     

    Windows系统API函数

    timeGetTime()、GetTickCount()及QueryPerformanceCounter()

     

    DWORD timeGetTime(VOID);

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

    头文件:#include <Mmsystem.h>            

    引用库:#pragma comment(lib, "Winmm.lib")  

     

    DWORD WINAPI GetTickCount(void);

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

    头文件:直接使用#include <windows.h>就可以了。

     

    高精度计时,以微秒为单位(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()所得的计时器值之差,用差去除以频率就得到精确的计时了。

    头文件:直接使用#include <windows.h>就可以了。

    转载请标明出处,原文地址:http://blog.csdn.net/morewindows/article/details/6854764

    View Code
     1 //Windows系统下time(),clock(),timeGetTime(),GetTickCount(),QueryPerformanceCounter()来计时 by MoreWindows   
    2 #include <stdio.h>
    3 #include <windows.h>
    4 #include <time.h> //time_t time() clock_t clock()
    5 #include <Mmsystem.h> //timeGetTime()
    6 #pragma comment(lib, "Winmm.lib") //timeGetTime()
    7
    8 int main()
    9 {
    10 //用time()来计时 秒
    11 time_t timeBegin, timeEnd;
    12 timeBegin = time(NULL);
    13 Sleep(1000);
    14 timeEnd = time(NULL);
    15 printf("%d\n", timeEnd - timeBegin);
    16
    17
    18 //用clock()来计时 毫秒
    19 clock_t clockBegin, clockEnd;
    20 clockBegin = clock();
    21 Sleep(800);
    22 clockEnd = clock();
    23 printf("%d\n", clockEnd - clockBegin);
    24
    25
    26 //用timeGetTime()来计时 毫秒
    27 DWORD dwBegin, dwEnd;
    28 dwBegin = timeGetTime();
    29 Sleep(800);
    30 dwEnd = timeGetTime();
    31 printf("%d\n", dwEnd - dwBegin);
    32
    33
    34 //用GetTickCount()来计时 毫秒
    35 DWORD dwGTCBegin, dwGTCEnd;
    36 dwGTCBegin = GetTickCount();
    37 Sleep(800);
    38 dwGTCEnd = GetTickCount();
    39 printf("%d\n", dwGTCEnd - dwGTCBegin);
    40
    41
    42 //用QueryPerformanceCounter()来计时 微秒
    43 LARGE_INTEGER large_interger;
    44 double dff;
    45 __int64 c1, c2;
    46 QueryPerformanceFrequency(&large_interger);
    47 dff = large_interger.QuadPart;
    48 QueryPerformanceCounter(&large_interger);
    49 c1 = large_interger.QuadPart;
    50 Sleep(800);
    51 QueryPerformanceCounter(&large_interger);
    52 c2 = large_interger.QuadPart;
    53 printf("本机高精度计时器频率%lf\n", dff);
    54 printf("第一次计时器值%I64d 第二次计时器值%I64d 计时器差%I64d\n", c1, c2, c2 - c1);
    55 printf("计时%lf毫秒\n", (c2 - c1) * 1000 / dff);
    56
    57 printf("By MoreWindows\n");
    58 return 0;
    59 }
  • 相关阅读:
    【vue坑】vue组件不显示,没加载dom
    vue radio绑定数据
    git修改密码遇到的坑 git -- Authentication failed for
    python全局变量
    adb无法使用,提示error: unknown host service的解决办法
    uiautomator2 init初始化失败
    【解决方案】安装lxml失败 Installing lxml
    【一般都是源的问题】ubuntu使用apt-get update更新失败
    ubuntu 安装python3.6 以及安装pip3 出现Command '('lsb_release', '-a')' returned non-zero exit status 1问题解决
    ubuntu设置python软链python3.5和python3.6同时存在,python3指令使用python3.6
  • 原文地址:https://www.cnblogs.com/dorothychai/p/2383199.html
Copyright © 2020-2023  润新知