简体   繁体   English

C - crossplatform中函数的毫秒精度计时

[英]millisecond precision timing of functions in C - crossplatform

Is there a way to get milliseconds precision, accurate (at least within a few ms) times in C using a cross-platform approach? 有没有办法使用跨平台方法在C中获得毫秒精度,准确度(至少在几毫秒内)?

on a POSIX system I can use sys/time.h, but that is not cross-platform. 在POSIX系统上我可以使用sys / time.h,但这不是跨平台的。

the stdlib time() function only gives second level precision stdlib time()函数仅提供二级精度

I haven't found a cross-platform solution to measuring time in C, per se. 我还没有找到一个跨平台的解决方案来测量C本身的时间。 However, what I do is use almost identical functions for Unix and Windows. 但是,我所做的是为Unix和Windows使用几乎相同的功能。 I created this gist because I always have to re-look this up every time. 我创造了这个要点因为我每次都要重新审视这个。 In short: 简而言之:

Unix Unix的

#include <time.h>

long diff_micro(struct timespec *start, struct timespec *end)
{
    /* us */
    return ((end->tv_sec * (1000000)) + (end->tv_nsec / 1000)) -
        ((start->tv_sec * 1000000) + (start->tv_nsec / 1000));
}

long diff_milli(struct timespec *start, struct timespec *end)
{
    /* ms */
    return ((end->tv_sec * 1000) + (end->tv_nsec / 1000000)) -
        ((start->tv_sec * 1000) + (start->tv_nsec / 1000000));
}

int main(int argc, char **argv)
{
    struct timespec start, end;

    clock_gettime(CLOCK_MONOTONIC, &start);

    // Activity to be timed
    sleep(1000);

    clock_gettime(CLOCK_MONOTONIC, &end);

    printf("%ld us\n", diff_micro(&start, &end));
    printf("%ld ms\n", diff_milli(&start, &end));

    return 0;
}

source for Unix solution Unix解决方案的源代码

Win32 Win32的

#include <Windows.h>

long diff_micro(LARGE_INTEGER *start, LARGE_INTEGER *end)
{
    LARGE_INTEGER Frequency, elapsed;

    QueryPerformanceFrequency(&Frequency); 
    elapsed.QuadPart = end->QuadPart - start->QuadPart;

    elapsed.QuadPart *= 1000000;
    elapsed.QuadPart /= Frequency.QuadPart;

    return elapsed.QuadPart;
}

long diff_milli(LARGE_INTEGER *start, LARGE_INTEGER *end)
{
    LARGE_INTEGER Frequency, elapsed;

    QueryPerformanceFrequency(&Frequency); 
    elapsed.QuadPart = end->QuadPart - start->QuadPart;

    elapsed.QuadPart *= 1000;
    elapsed.QuadPart /= Frequency.QuadPart;

    return elapsed.QuadPart;
}

int main(int argc, char **argv)
{
    LARGE_INTEGER StartingTime, EndingTime;

    QueryPerformanceCounter(&StartingTime);

    // Activity to be timed
    Sleep(1000);

    QueryPerformanceCounter(&EndingTime);

    printf("%ld us\n", diff_micro(&StartingTime, &EndingTime));
    printf("%ld ms\n", diff_milli(&StartingTime, &EndingTime));

    return 0;
}

source used for Win32 solution 用于Win32解决方案的源代码

You can try something like this:- 你可以尝试这样的事情: -

#include <time.h>
clock_t uptime = clock() / (CLOCKS_PER_SEC / 1000);

See this Link 请参阅此链接

The best way is using std::chrono 最好的方法是使用std :: chrono

#include <chrono>

...

auto begin = std::chrono::high_resolution_clock::now();

...

auto end = std::chrono::high_resolution_clock::now();
elapsedTime = std::chrono::duration_cast<std::chrono::milliseconds>(end - begin).count();

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

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