C语言 在 C 代码中以纳秒为单位计算函数时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13610471/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:36:20  来源:igfitidea点击:

Calculating Function time in nanoseconds in C code

cwindowstime

提问by Mousa Farajallah

I need to know how can I calculate the time of a function in C code in nanoseconds. I tried to repeat the function until consume some microseconds. Are there any other functions in time.hthat can be used to calculate the time in nanoseconds?

我需要知道如何以纳秒为单位计算 C 代码中函数的时间。我试图重复该功能,直到消耗一些微秒。是否有任何其他函数time.h可用于计算以纳秒为单位的时间?

回答by chacham15

You are never going to get nanosecond accuracy. Think about what you are asking: on a 1 GHz CPU 1 nanosecond is a clock cycle. No matter what you attempt to call, you will never get that kind of accuracy, you are better off sticking to microseconds. A similar question with many examples is here: C++ Cross-Platform High-Resolution Timer.

你永远不会获得纳秒精度。想想你在问什么:在 1 GHz CPU 上,1 纳秒是一个时钟周期。无论你试图调用什么,你永远不会得到那种准确度,你最好坚持微秒。一个有很多例子的类似问题在这里:C++ Cross-Platform High-Resolution Timer

For c only: on windows you want to use the QueryPerformanceCounter. And here is more on QPC. Hereis a related question on how to use QueryPerformanceCounter.

仅适用于 c:在 Windows 上,您要使用QueryPerformanceCounter。这里有更多关于QPC 的内容是有关如何使用 QueryPerformanceCounter 的相关问题。

回答by Cloud

Regardless of how you approach this or what type of system/OS you are using, you are getting an approximate answer at best, with considerable variance due to the nature of the problem.

无论您如何处理此问题或使用何种类型的系统/操作系统,您最多只能得到一个近似答案,但由于问题的性质而存在相当大的差异。

Second, you need a system that supports this kind of call. It's pretty easy if you're using QNX Neutrino:

其次,您需要一个支持这种调用的系统。如果您使用 QNX Neutrino,这很容易:

http://www.qnx.com/developers/docs/6.3.0SP3/neutrino/lib_ref/c/clock_gettime.html

http://www.qnx.com/developers/docs/6.3.0SP3/neutrino/lib_ref/c/clock_gettime.html

/*
 * This program calculates the time required to
 * execute the program specified as its first argument.
 * The time is printed in seconds, on standard out.
 */
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>

#define BILLION  1000000000L;

int main( int argc, char** argv )
  {
    struct timespec start, stop;
    double accum;

    if( clock_gettime( CLOCK_REALTIME, &start) == -1 ) {
      perror( "clock gettime" );
      return EXIT_FAILURE;
    }

    system( argv[1] );

    if( clock_gettime( CLOCK_REALTIME, &stop) == -1 ) {
      perror( "clock gettime" );
      return EXIT_FAILURE;
    }

    accum = ( stop.tv_sec - start.tv_sec )
             + (double)( stop.tv_nsec - start.tv_nsec )
               / (double)BILLION;
    printf( "%lf\n", accum );
    return EXIT_SUCCESS;
  }

回答by R.. GitHub STOP HELPING ICE

The clockfunction in standard C is not useful for this. It usually has horrible resolution and it's inconsistent (between platforms) whether it measures elapsed wall time or cpu time consumed. You should use the POSIX-standard clock_gettimefunction (which has nanosecond resolution and lets you specify which clock you want to measure against) and emulate it with whatever system-specific clock operations are available on platforms that lack the POSIX function.

clock标准 C 中的函数对此没有用处。它通常具有可怕的分辨率,并且(在平台之间)无论是测量经过的墙壁时间还是消耗的 CPU 时间都不一致。您应该使用 POSIX 标准clock_gettime函数(它具有纳秒分辨率,并允许您指定要测量的时钟)并使用在缺少 POSIX 函数的平台上可用的任何特定于系统的时钟操作来模拟它。

回答by Alex I

Call the function enough times that you get total time in the seconds, and use any method to measure (even plain C clock()). Measuring in micro/nanoseconds is inherently too imprecise.

调用该函数足够多的次数,以获得以秒为单位的总时间,并使用任何方法进行测量(甚至是普通的 C 时钟())。以微/纳秒为单位进行测量本质上过于不精确。

For benchmarking, you want QueryPerformanceCounter. It is the best stopwatch available on Windows. See: How to use QueryPerformanceCounter?However using this to measure (for example) a single function call will still be inherently very imprecise; I think you need to time on the order of a second total to get good signal-to-noise ratio for your measurements. Take multiple measurements of whatever duration you want and compare their value to their standard deviation to make sure you are measuring sufficiently accurately.

对于基准测试,您需要QueryPerformanceCounter。它是 Windows 上最好的秒表。请参阅:如何使用 QueryPerformanceCounter?然而,使用它来衡量(例如)单个函数调用本质上仍然非常不精确;我认为您需要大约一秒钟的时间才能为您的测量获得良好的信噪比。对您想要的任何持续时间进行多次测量,并将它们的值与其标准偏差进行比较,以确保您的测量足够准确。

Multimedia Timersare probably the best clock (note the difference between clock and stopwatch: one measures absolute time, the other time intervals only) available on Windows. You should be able to get near-millisecond resolution and precision with timeGetTime().

多媒体计时器可能是 Windows 上可用的最佳时钟(请注意时钟和秒表之间的区别:一个测量绝对时间,另一个仅测量时间间隔)。您应该能够使用timeGetTime()获得接近毫秒的分辨率和精度。