【问题标题】:Some issues in C++ performance measurementC++性能测量中的一些问题
【发布时间】:2013-11-04 22:39:33
【问题描述】:
 timespec start, end;
 clock_gettime(CLOCK_MONOTONIC, &start);
 ---code--lookup in a unordered map <string, int>
 clock_gettime(CLOCK_MONOTONIC, &end);
 int diff = diff_ns(end, start);

Results:
 SAMPLE   MIN(ns)   MAX(ns)   AVG(ns)
           100      1000      3000      1430
           500      1000      2000      1436
          1000         0     16000      1441
          5000         0     15000      1479
         10000         0     26000      1489
         50000         0    363000      1589
        100000         0    110000      1591
        200000         0    804000      1659
        300000         0    118000      1668
        400000      1000    354000      1701
        500000         0   8679000      1712
        600000         0    809000      1701
        700000         0    373000      1704
        800000         0    850000      1716
        900000         0    856000      1736
       1000000         0    817000      1730
  • 如何忽略 CPU 计算clock_gettime 所花费的时间,因为最后我们也花费了clock_gettime 调用所花费的时间?

  • 我在单线程程序中运行测试...但是如何确保没有发生上下文切换,因为其他进程也可能在 VM 上运行

  • 有时我会得到零时间,因为我以纳秒为单位进行测量,我觉得很奇怪,怎么能在零纳秒内执行?

【问题讨论】:

    标签: c++ linux optimization profiling performance-testing


    【解决方案1】:

    我正在单线程程序中运行测试...但是如何确保没有发生上下文切换,因为其他进程也可能在 VM 上运行

    杀死所有不需要的进程,如果可以选择,提高分析进程的优先级。

    除此之外,您可以使用profcallgrind 来分析您的程序。

    有时我会得到零时间,因为我以纳秒为单位进行测量,我觉得很奇怪,怎么能在零纳秒内执行?

    执行时间为 0 ns,因为 CPU 时钟精度高于 10 毫秒。

    在更多的迭代之后测量时间,你会得到更好的结果。


    做更多的查找,然后取平均值:

     timespec start, end;
     clock_gettime(CLOCK_MONOTONIC, &start);
     for (int i=0;i<10000;++i)
     ---code--lookup in a unordered map <string, int>
     clock_gettime(CLOCK_MONOTONIC, &end);
     int diff = diff_ns(end, start)/10000;
    

    像这样在clock_gettime中花费的时间会被忽略。

    【讨论】:

    • 如果我每 n 次迭代测量,我如何计算最小值/最大值。
    • @Medicine 进行更多查找,然后平均该值。如果您只执行一次无序地图查找,则改为执行 N,并将测量时间除以 N - 其中 N 是一个很大的数字 (10000+)
    • @BЈовић 知道了。谢谢。
    猜你喜欢
    • 2012-07-26
    • 2011-09-13
    • 2020-01-05
    • 1970-01-01
    • 1970-01-01
    • 2014-05-21
    • 1970-01-01
    • 1970-01-01
    • 2010-12-04
    相关资源
    最近更新 更多