代码的高分辨率计时部分

发布于 2024-07-15 12:05:21 字数 822 浏览 5 评论 0原文

我想测量循环内函数的速度。 但为什么我的做法总是打印“0”而不是具有 9 位小数精度(即纳秒/微秒)的高分辨率计时?

正确的做法是什么?

#include <iomanip>
#include <iostream>
#include <time.h>
int main() {


 for (int i = 0; i <100; i++) {
    std::clock_t startTime = std::clock(); 
    // a very fast function in the middle
    cout << "Time: " << setprecision(9) << (clock() - startTime + 0.00)/CLOCKS_PER_SEC << endl;
 }

 return 0;
}

相关问题:

I want to measure the speed of a function within a loop. But why my way of doing it always print "0" instead of high-res timing with 9 digits decimal precision (i.e. in nano/micro seconds)?

What's the correct way to do it?

#include <iomanip>
#include <iostream>
#include <time.h>
int main() {


 for (int i = 0; i <100; i++) {
    std::clock_t startTime = std::clock(); 
    // a very fast function in the middle
    cout << "Time: " << setprecision(9) << (clock() - startTime + 0.00)/CLOCKS_PER_SEC << endl;
 }

 return 0;
}

Related Questions:

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

堇年纸鸢 2024-07-22 12:05:21

将时间计算函数移到 for () { .. } 语句之外,然后将总执行时间除以测试循环中的操作数。

#include <iostream>
#include <ctime>
#define NUMBER 10000 // the number of operations

// get the difference between start and end time and devide by
// the number of operations
double diffclock(clock_t clock1, clock_t clock2)
{
    double diffticks = clock1 - clock2;
    double diffms = (diffticks) / (CLOCKS_PER_SEC / NUMBER);
    return diffms;
}

int main() {
    // start a timer here
    clock_t begin = clock();

    // execute your functions several times (at least 10'000)
    for (int i = 0; i < NUMBER; i++) {
        // a very fast function in the middle
        func()
    }

    // stop timer here
    clock_t end = clock();

    // display results here
    cout << "Execution time: " << diffclock(end, begin) << " ms." << endl;
    return 0;
}

注意:std::clock() 缺乏足够的精度来进行分析。 参考

Move your time calculation functions outside for () { .. } statement then devide total execution time by the number of operations in your testing loop.

#include <iostream>
#include <ctime>
#define NUMBER 10000 // the number of operations

// get the difference between start and end time and devide by
// the number of operations
double diffclock(clock_t clock1, clock_t clock2)
{
    double diffticks = clock1 - clock2;
    double diffms = (diffticks) / (CLOCKS_PER_SEC / NUMBER);
    return diffms;
}

int main() {
    // start a timer here
    clock_t begin = clock();

    // execute your functions several times (at least 10'000)
    for (int i = 0; i < NUMBER; i++) {
        // a very fast function in the middle
        func()
    }

    // stop timer here
    clock_t end = clock();

    // display results here
    cout << "Execution time: " << diffclock(end, begin) << " ms." << endl;
    return 0;
}

Note: std::clock() lacks sufficient precision for profiling. Reference.

野心澎湃 2024-07-22 12:05:21

一些提示:

  1. 我会小心优化器,如果我认为它不做任何事情,它可能会抛出所有代码。
  2. 您可能想要运行循环 100000 次。
  3. 在进行总时间计算之前,将当前时间存储在变量中。
  4. 多次运行您的程序。

A few pointers:

  1. I would be careful with the optimizer, it might throw all your code if I will think that it doesn't do anything.
  2. You might want to run the loop 100000 times.
  3. Before doing the total time calc store the current time in a variable.
  4. Run your program several times.
情释 2024-07-22 12:05:21

如果您需要更高的分辨率,唯一的方法是依赖于平台。

在 Windows 上,请查看 QueryPerformanceCounter/QueryPerformanceFrequency API。

在 Linux 上,查找 clock_gettime()

If you need higher resolution, the only way to go is platform dependent.

On Windows, check out the QueryPerformanceCounter/QueryPerformanceFrequency API's.

On Linux, look up clock_gettime().

眼趣 2024-07-22 12:05:21

请参阅我就同一件事提出的问题:显然 clock() 的分辨率不能保证如此高。

C++ 在 Linux 上获取毫秒时间 --clock() 似乎无法正常工作

尝试 gettimeofday函数,或 boost

See a question I asked about the same thing: apparently clock()'s resolution is not guaranteed to be so high.

C++ obtaining milliseconds time on Linux -- clock() doesn't seem to work properly

Try gettimeofday function, or boost

給妳壹絲溫柔 2024-07-22 12:05:21

如果您需要平台独立性,则需要使用 ACE_High_Res_Timer (http://www.dre.vanderbilt.edu/Doxygen/5.6.8/html/ace/a00244.html

If you need platform independence you need to use something like ACE_High_Res_Timer (http://www.dre.vanderbilt.edu/Doxygen/5.6.8/html/ace/a00244.html)

勿忘初心 2024-07-22 12:05:21

您可能想考虑使用 openMp。

#include <omp.h>

int main(int argc, char* argv[])
{       
    double start = omp_get_wtime();

    // code to be checked

    double end = omp_get_wtime();

    double result = end - start;

    return 0;
}

You might want to look into using openMp.

#include <omp.h>

int main(int argc, char* argv[])
{       
    double start = omp_get_wtime();

    // code to be checked

    double end = omp_get_wtime();

    double result = end - start;

    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文