您将如何对功能的性能进行基准测试? [英] How would you benchmark the performance of a function?

查看:89
本文介绍了您将如何对功能的性能进行基准测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这也许是一个更高级的问题.如果您有两个返回值的函数,

Here's perhaps a more advanced question. If you have two functions that return a value,

int F(int input1, int input2)
{
    int output;
    // <Some algorithm that assigns value to output>
    return output;
}

int D(int input1, int input2)
{
    int output;
    // <Another algorithm that assigns value to output>
    return output;
}

条件为 F(a,b)== D(a,b)(对于相同的输入,两者都返回相同的值).

With the condition that F(a,b) == D(a,b) (both return the same value for the same inputs).

如果您想对它们的性能进行基准测试,您将如何做? 更精确地讲,您如何隔离执行 F(a,b) D(a,b)所需的时间,以使其不会 strong>是否反映了基准设置中 other 次要操作所花费的时间?

If you'd like to benchmark their performance, how would you do it? More precisely, how would you isolate the time it takes to perform F(a,b) or D(a,b) such that it does not reflect the time it takes for the other secondary operations in the benchmark setup?

推荐答案

最好的开源解决方案之一是 Google基准.

One of the best available opensource solutions is Google Benchmark.

您必须围绕要进行基准测试的代码创建简单的包装程序,并将其静态或动态链接到基准测试库.在您的代码附近编译此类微型基准测试通常很有用.要获取灵感,请参见很棒的演示文稿.

You have to create simple wrappers around code you want to benchmark and link either statically or dynamically with the benchmark library. It is often useful to have such micro benchmarks compiled near with your code. For inspiration see awesome presentation.

static void BM_F(benchmark::State& state) {
  const auto input1 = state.range_x();
  const auto input2 = state.range_y();

  while (state.KeepRunning()) F(input1, input2);
}

static void BM_D(benchmark::State& state) {
  const auto input1 = state.range_x();
  const auto input2 = state.range_y();

  while (state.KeepRunning()) D(input1, input2);
}

BENCHMARK(BM_F)
    ->ArgPair(1, 10)
    ->ArgPair(10, 100)
    ->ArgPair(100, 1000);

BENCHMARK(BM_D)
    ->ArgPair(1, 10)
    ->ArgPair(10, 100)
    ->ArgPair(100, 1000);

如果要测量原始CPU周期,则唯一的选择是使用直接CPU指令.对于x86,您可以使用时间戳计数器.

If you want to measure raw CPU cycles, then your only choice is to use direct CPU instructions. For x86 you can use Time Stamp Counter.

但是您应该知道,这样的测量不会抵抗OS执行的任何上下文切换或在CPU上跳转.在这种情况下,您唯一的选择是使用具有单一执行流程的算法.在进入测试功能之前,请记住CPU的ID和TSC值,并在测试功能之后检查CPU的ID.然后计算TSC值之间的差.您还可以为您的进程设置CPU亲和力,以将进程绑定到特定的CPU.

But you should be aware, that such measuring will not resist any context switches performed by OS or jumping on CPUs. Your only choice in such situations will be to use an algorithm with a single flow of execution. Remember the ID of the CPU and the TSC value before entering to test function, and check the ID of the CPU after the test function. Then calculating the difference between TSC values. You may also set up CPU affinity for your process to stick the process to a specific CPU.

另一种特定于Linux的基准测试功能的可能方法是使用 perf工具.

Another Linux-specific possible way to benchmark functions is to use perf tool.

但是在任何情况下,任何测量都会为结果增加一定的误差水平.

But in any case, any measurement will add some error level to the result.

这篇关于您将如何对功能的性能进行基准测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆