在Windows下获取毫秒级运行时间的方法

  1. 头文件:<Windows.h>
  2. 函数原型:
    /*获取时钟频率,保存在结构LARGE_INTEGER中***/
    WINBASEAPI
    BOOL
    WINAPI
    QueryPerformanceFrequency(
    _Out_ LARGE_INTEGER * lpFrequency
    );
    /*获取从某个时间点开始的时钟周期数,保存在结构LARGE_INTEGER中**/
    WINBASEAPI
    BOOL
    WINAPI
    QueryPerformanceFrequency(
    _Out_ LARGE_INTEGER * lpFrequency
    );
  3. LARGE_INTEGER结构
    typedef union _LARGE_INTEGER {
    struct {
    DWORD LowPart;
    LONG HighPart;
    } DUMMYSTRUCTNAME;
    struct {
    DWORD LowPart;
    LONG HighPart;
    } u;
    #endif //MIDL_PASS
    LONGLONG QuadPart;
    } LARGE_INTEGER;

    LARGE_INTEGER为一个union,我们将使用成员QuadPart获取时钟周期数。

  4. 方法:

    1) 调用QueryPerformanceFrequency()获取时钟频率

    2) 在待测部分的首尾分别调用QueryPerformanceCounter()获取两个时间点的时钟周期数

    3) 将两个节点的时钟周期数差值除以时钟频率即可得到测试部分的运行时间

    参考代码:

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <Windows.h>
#define SIZE 100
using namespace std;
int a[SIZE];
int b[SIZE];
int c[SIZE];
int main(){
//srand(time(0));
for (int i = ; i < SIZE; ++i){
a[i] = rand();
b[i] = rand();
}
LARGE_INTEGER begain, end, frequency;
QueryPerformanceFrequency(&frequency);
QueryPerformanceCounter(&begain);
#pragma omp parallel for
for (int i = ; i < SIZE; ++i){
c[i] = a[i] * b[i];
}
QueryPerformanceCounter(&end);
cout << "Cost time : " << (double)(end.QuadPart - begain.QuadPart) * / frequency.QuadPart << "ms" << endl;
cout << begain.QuadPart << endl;
}

在Linux下获取毫秒级运行时间的方法

  1. 头文件<sys/time.h>
  2. 函数原型:
    int gettimeofday(struct timeval *tv, struct timezone *tz);
    struct timeval {
    time_t tv_sec; /* seconds */
    suseconds_t tv_usec; /* microseconds */
    };

    参考代码:

#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
int main()
{
struct timeval _tstart, _tend;
double t1, t2; gettimeofday(&_tstart, NULL); // ToDo.. gettimeofday(&_tend, NULL); t1 = (double)_tstart.tv_sec * + (double)_tstart.tv_usec / ;
t2 = (double)_tend.tv_sec * + (double)_tend.tv_usec / ;
printf("Cost time : %fms\n", t2 - t1);
return ;
}

参考资料:http://www.ibm.com/developerworks/cn/linux/sdk/rt/part1/index.html

在Windows及Linux下获取毫秒级运行时间的方法的更多相关文章

  1. Linux下得到毫秒级时间--C语言实现(转-度娘818)

    Linux下得到毫秒级时间--C语言实现 原文链接: http://www.cnblogs.com/nwf5d/archive/2011/06/03/2071247.html #ifdef HAVE_ ...

  2. linux下获取微秒级精度的时间【转】

    转自:https://blog.csdn.net/u011857683/article/details/81320052 使用C语言在linux环境下获得微秒级时间 1. 数据结构 int getti ...

  3. 怎样在windows下和linux下获取文件(如exe文件)的具体信息和属性

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/xmt1139057136/article/details/25620685 程序猿都非常懒.你懂的! ...

  4. Windows访问Linux下的共享目录的配置方法

    user安全级别 第一步:安装samba3(如果已经安装就跳过这一步)  [root@rhce2 /]# yum groupinstall "CIFS file server" 第 ...

  5. windows和linux下获取当前程序路径以及cpu数

    #ifdef WIN32 #include <Windows.h> #else #include <stdio.h> #include <unistd.h> #en ...

  6. socket在windows下和linux下的区别

    原文:socket在windows下和linux下的区别 1)头文件 windows下winsock.h/winsock2.h linux下sys/socket.h    错误处理:errno.h 2 ...

  7. Linux下的微秒级定时器: usleep, nanosleep, select, pselect

    Linux下的微秒级定时器: usleep, nanosleep, select, pselect 标签: linuxnulldelaystructdate 2012-02-07 23:29 4979 ...

  8. Windows与Linux下文件操作监控的实现

    一.需求分析: 随着渲染业务的不断进行,数据传输渐渐成为影响业务时间最大的因素.究其原因就是因为数据传输耗费较长的时间.于是,依托于渲染业务的网盘开发逐渐成为迫切需要解决的需求.该网盘的实现和当前市场 ...

  9. .net core在Linux下获取AD域信息

    .net core在Linux下获取AD域信息 .net Core 2.1.4 .net core现在System.DirectoryServices只支持Windows平台下使用. 参考: http ...

随机推荐

  1. struts框架的一些注意点

    1.Struts.xml文件中<include file="">标签的运用 用法:此标签引用配置文件,Struts2提供了一个默认的struts.xml文件,当此配置文 ...

  2. Hibernate使用时需要注意的几个小问题

    今天晚上玩了一下JDBC连接数据库,之后又利用Hibernate进行了数据库的访问,感觉利用Hibernate对数据库访问在文件配置好了之后确实更加简单快捷. 但是在操作的过程中也有一些细节需要注意一 ...

  3. web开发jsp页面遇到的一系列问题

    一:web总结 1.jsp页面知识点巩固 1.1字符串数字格式化转换 <%@ taglib prefix="fmt" uri="http://java.sun.co ...

  4. 深入理解DiscoveryClient

    Spring Cloud Commons 提供的抽象 最早的时候服务发现注册都是通过DiscoveryClient来实现的,随着版本变迁把DiscoveryClient服务注册抽离出来变成了Servi ...

  5. LTP安装方法

    git clone https://github.com/linux-test-project/ltp.git apt-get install automake make autotools ./co ...

  6. F Find the AFei Numbers

    链接:https://ac.nowcoder.com/acm/contest/338/F来源:牛客网 题目描述 AFei loves numbers. He defines the natural n ...

  7. eclipsePreferences位置

    1.Windows:菜单栏-Window-Preferences 2.Mac:应用顶部最左侧Eclipse-Preferences ---------------------------------- ...

  8. Source Insight symbol not found

    使用SourceInsight查看源代码时,发现点击查看相关类型时,无法关联到其代码,出现 symbol not found, 然而明明在我的头文件有定义的 网上查了一下主要是因为新建工程导入文件后, ...

  9. ES6——函数-参数

    函数的参数: 1.参数扩展/数组展开      1)收集(剩余的)参数          function show(a,b,...args){}   // 三点运算符           *Rest ...

  10. 2019-9-2-win10-uwp-标题栏

    title author date CreateTime categories win10 uwp 标题栏 lindexi 2019-09-02 12:57:38 +0800 2018-2-13 17 ...