写代码时,经常会计算某一段代码的运行时间,以下提供一个微秒级别的类供参考

class CTimeCost
{
public:
CTimeCost(const string &str)
: m_str(str)
{
QueryPerformanceFrequency(&m_freg);
QueryPerformanceCounter(&m_begin);
} ~CTimeCost()
{
LARGE_INTEGER end;
QueryPerformanceCounter(&end);
__int64 nTick = (end.QuadPart - m_begin.QuadPart) * * / m_freg.QuadPart; int len = ;
char *buf = new char[len + ];
memset(buf, , len + );
sprintf_s(buf, len, "\n%s cost %ld微秒\n", m_str.c_str(), nTick); OutputDebugStringA(buf);
cout << buf << endl; delete[]buf;
buf = NULL;
} private:
string m_str;
LARGE_INTEGER m_begin;
LARGE_INTEGER m_freg;
};

  //使用方法
void TestTimeCost()
{
{
CTimeCost once("this block cost");
Sleep();
} cout << "other code run" << endl;
}

windows下C++高精度计时的更多相关文章

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

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

  2. linux 和 windows下的程序计时

    Windows 使用<windows.h>中的GetTickCount(),该函数获得从操作系统启动到现在所经过(elapsed)的毫秒数,它的返回值是DWORD. 转自:http://w ...

  3. Windows下获取高精度时间注意事项 [转贴 AdamWu]

    花了很长时间才得到的经验,与大家分享. 1. RDTSC - 粒度: 纳秒级 不推荐优势: 几乎是能够获得最细粒度的计数器抛弃理由: A) 定义模糊 - 曾经据说是处理器的cycle counter, ...

  4. windows 下,用CreateWaitableTimer SetWaitableTimer 创建定时器(用轮询的办法保持高精度)

    windows 下,用CreateWaitableTimer SetWaitableTimer 创建定时器可以有 100 纳秒也就是 1/10 微秒, 1/10000 毫秒的精度. 呵呵. SetWa ...

  5. c++ windows下计时

    多核时代不宜再用 x86 的 RDTSC 指令测试指令周期和时间 陈硕Blog.csdn.net/Solstice 自从 Intel Pentium 加入 RDTSC 指令以来,这条指令是 micro ...

  6. C#下利用高精度计时器进行计时操作

    简介 精确的时间计量方法在某些应用程序中是非常重要的.常用的 Windows API 方法 GetTickCount() 返回系统启动后经过的毫秒数.另一方面,GetTickCount() 函数仅有 ...

  7. windows下实现微秒级的延时

    windowsintegeriostream汇编嵌入式任务 最近正在做一个嵌入式系统,是基于windows ce的,外接硬件的时序要微秒级的延时.1.微秒级的延时肯定不能基于消息(SetTimer函数 ...

  8. [转]使用Stopwatch类实现高精度计时

    对一段代码计时同查通常有三种方法.最简单就是用DateTime.Now来进行比较了,不过其精度只有3.3毫秒,可以通过DllImport导入QueryPerformanceFrequency和Quer ...

  9. 【转载】c/c++在windows下获取时间和计算时间差的几种方法总结

    一.标准C和C++都可用 1.获取时间用time_t time( time_t * timer ),计算时间差使用double difftime( time_t timer1, time_t time ...

随机推荐

  1. 【Java并发系列04】线程锁synchronized和Lock和volatile和Condition

    img { border: solid 1px } 一.前言 多线程怎么防止竞争资源,即防止对同一资源进行并发操作,那就是使用加锁机制.这是Java并发编程中必须要理解的一个知识点.其实使用起来还是比 ...

  2. HDU5988 Coding Contest(费用流)

    2016青岛现场赛的一题,由于第一次走过不会产生影响,需要拆点,不过比赛时没想到,此外还有许多细节要注意,如要加eps,时间卡得较紧要注意细节优化等 #include <iostream> ...

  3. 12.Java中Comparable接口,Readable接口和Iterable接口

    1.Comparable接口 说明:可比较(可排序的) 例子:按照MyClass的y属性进行生序排序 class MyClass implements Comparable<MyClass> ...

  4. 【Java 新建项目】使用程序对新项目的各个实体 创建Dao、DaoImpl、Service、ServiceImpl层的文件

    首先给出基本Dao层代码: GenericDao.java package com.agen.dao; import java.io.Serializable; import java.util.Co ...

  5. [leetcode] 小心成环

    156. Binary Tree Upside Down Given a binary tree where all the right nodes are either leaf nodes wit ...

  6. 号称21世纪的编辑器Atom

    上个月无意中在一篇软文中看到一篇前端排行榜,其中有一项排行就是编辑器,而排在前三的编辑器分别是sublime.Atom.webstorm.出于好奇,简单的在网上查看了介绍,原来全球最大开源分享网站gi ...

  7. 如何找出标有"App Store 精华","Essentials"的所有软件?

    如何找出标有"App Store 精华","Essentials"的所有软件? 中国区: +"App Store 精华" site:http ...

  8. Hackerrank11 LCS Returns 枚举+LCS

    Given two strings,  a and , b find and print the total number of ways to insert a character at any p ...

  9. php操作mysql

  10. jQuery参数学习与整理

    bind---可同时为元素嵌套多个事件. blur---当输入框焦点失去时发生的事件(获得焦点参数focus与之同理) change---当元素值改变时发生的事件 click---单击事件 dbcli ...