5 次方运算比 switch 语句执行得更快 [英] 5th power operation performs quicker than switch statement

查看:28
本文介绍了5 次方运算比 switch 语句执行得更快的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在解决一个问题,我需要返回数字 0-9 的五次方,我以为我可以通过切换来加速程序

I'm working on a problem where I need to return the fifth power of a digit 0-9 and I had thought I could speed up the program by switching

int pow(int a){return a*a*a*a*a;}

出去

int pow(int a){
switch(a){
case 0: return 0; break;
case 1: return 1;  break;
case 2: return 32;  break;
case 3: return 243;  break;
case 4: return 1024;  break;
case 5: return 3125;  break;
case 6: return 7776;  break;
case 7: return 16807;  break;
case 8: return 32768;  break;
case 9: return 59049;  break;}
return 0;}

但我意识到使用第一个函数时程序的运行速度比使用第二个函数快 20%,尽管第一个函数需要 5 个乘法运算,而第二个函数只调用一个 switch 语句,这是为什么呢?

but I realized that the program runs about 20% faster with the first function than it does with the second, despite the first requiring 5 multiplication operations and the second only invokes a single switch statement, why is this?

推荐答案

它并不像你想象的那样枯燥乏味.根据您的输入,两者都可以更快.在这种情况下,如果您反复查找相同的值,则查表速度会更快.如果查找不同的值,乘法会更快.我猜这是分支预测器在每次查找时使用一个常量值来完成它的工作.

It's not as cut and dry as you make it out to be. Depending on your input, either can be faster. In this case, if you look the same value up repeatedly, the table lookup is faster. If you look up different values, the multiplication is faster. I'm guessing that this is the branch predictor doing its job on the lookup with a constant value each time.

忽略不同"的要高得多的事实 - 这就是模数的成本.只需将最左边的两个相互比较,然后将接下来的两个相互比较即可.

Ignore the fact that the "varying" ones are much higher - that's the cost of the modulus. Simply compare the leftmost two with each other and the next two with each other.

实时基准测试链接:http://quick-bench.com/uZLVxVIMxE21JTsHWJVN8Is-37I

生成的 ASM 进行基准测试显示在右下角的链接中.

The generated ASM being benchmarked is shown at that link in the bottom right.

int pow(int a){return a*a*a*a*a;}
int pow2(int a){
switch(a){
case 0: return 0; break;
case 1: return 1;  break;
case 2: return 32;  break;
case 3: return 243;  break;
case 4: return 1024;  break;
case 5: return 3125;  break;
case 6: return 7776;  break;
case 7: return 16807;  break;
case 8: return 32768;  break;
case 9: return 59049;  break;}
return 0;}



static void multiply_varying(benchmark::State& state) {
  // Code inside this loop is measured repeatedly
  volatile int i = 0;
  for (auto _ : state) {
    i = (i + 1) % 9;
    benchmark::DoNotOptimize(pow(i));
  }
}
// Register the function as a benchmark
BENCHMARK(multiply_varying);

static void lookup_varying(benchmark::State& state) {
  volatile int i = 5;
  for (auto _ : state) {
        i = (i + 1) % 9;
    benchmark::DoNotOptimize(pow2(i));
  }
}
BENCHMARK(lookup_varying);

static void multiply_constant(benchmark::State& state) {
  // Code inside this loop is measured repeatedly
  volatile int i = 5;
  for (auto _ : state) {
    benchmark::DoNotOptimize(pow(i));
  }
}
// Register the function as a benchmark
BENCHMARK(multiply_constant);


static void lookup_constant(benchmark::State& state) {
  volatile int i = 5;
  for (auto _ : state) {
    benchmark::DoNotOptimize(pow2(i));
  }
}
BENCHMARK(lookup_constant);

略有不同的基准在两种情况下的查找速度都更快:http://quick-bench.com/NRdzldykfQ8cQmGEn33FG0LMr2Q

edit: Slightly different benchmark has the lookup being faster in both cases: http://quick-bench.com/NRdzldykfQ8cQmGEn33FG0LMr2Q

这篇关于5 次方运算比 switch 语句执行得更快的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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