简体   繁体   English

在C ++中计算毫秒的便携方式?

[英]Portable way of counting milliseconds in C++?

Is there any portable (Windows & Linux) way of counting how many milliseconds elapsed between two calls ? 是否有任何便携式(Windows和Linux)方式计算两次呼叫之间经过了多少毫秒?

Basically, I want to achieve the same functionnality than the StopWatch class of .NET . 基本上,我想实现与.NETStopWatch类相同的功能。 (for those who already used it) (对于那些已经使用过它的人)

In a perfect world, I would have used boost::date_time but that's not an option here due to some silly rules I'm enforced to respect. 在一个完美的世界里,我会使用boost::date_time但这不是一个选项,因为我强制要遵守一些愚蠢的规则。

For those who better read code, this is what I'd like to achieve. 对于那些更好地阅读代码的人来说,这就是我想要实现的目标。

Timer timer;

timer.start();
// Some instructions here
timer.stop();

// Print out the elapsed time
std::cout << "Elapsed time: " << timer.milliseconds() << "ms" << std::endl;

So, if there is a portable (set of) function(s) that can help me implement the Timer class, what is it ? 那么,如果有一个可以帮助我实现Timer类的可移植(一组)函数,它是什么? If there is no such function, what Windows & Linux API should I use to achieve this functionnality ? 如果没有这样的功能,我应该用什么Windows和Linux API来实现这个功能呢? (using #ifdef WINDOWS -like macros) (使用#ifdef WINDOWS类似的宏)

Thanks ! 谢谢 !

On Linux (and generally in POSIX), you can use gettimeofday function, which returns number of microseconds since the Epoch. 在Linux上(通常在POSIX中),您可以使用gettimeofday函数,该函数返回自Epoch以来的微秒数。 On Windows, there is GetTickCount function, that return number of milliseconds since the system was started. 在Windows上,有GetTickCount函数,它返回自系统启动以来的毫秒数。

clock() (在Time.h中)返回一个每秒增加CLOCKS_PER_SEC的值,通常为1000。

On Windows, use the High Performance Timer, it's a doddle. 在Windows上,使用高性能计时器,这是一个轻而易举的事。

LARGE_INTEGER frequency;
LARGE_INTEGER one;
LARGE_INTEGER two;
QueryPerformanceFrequency(&frequency);
QueryPerformanceCounter(&one);
// something
QueryPerformanceCounter(&two);
std::cout << (((double)two.QuadPart - (double)one.QuadPart) / (double)frequency.QuadPart) * (double)1000;

In theory, this can go up to per-clock-cycle accuracy, depending on the CPU in question. 从理论上讲,这可以达到每个时钟周期的精度,具体取决于所讨论的CPU。

This is my code for this task (Boost license) Where ptime is class that represents time in seconds + nanoseconds ptime(int sec,int nano) 这是我执行此任务的代码(Boost许可证)其中ptime是表示以秒为单位的时间的类+纳秒ptime(int sec,int nano)

And ptime::microseconds create sec/nano pair from posix time microseconds. 并且ptime :: microseconds从posix时间微秒创建秒/纳米对。

It is quite easy to rewrite it for your needs and write such class. 根据您的需要重写它并编写这样的类非常容易。

ptime ptime::now()
{
    #ifndef BOOSTER_WIN_NATIVE
    struct timeval tv;
    gettimeofday(&tv,0);
    return ptime(tv.tv_sec,tv.tv_usec * 1000);
    #else
    FILETIME ft;
    GetSystemTimeAsFileTime(&ft);
    unsigned long long tt = ft.dwHighDateTime;
    tt <<=32;
    tt |= ft.dwLowDateTime;
    tt /=10;
    tt -= 11644473600000000ULL;
    return ptime(ptime::microseconds(tt));
    #endif
}

And there is no portable C++ function for this... 而且没有便携式C ++功能......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM