简体   繁体   English

性能函数调用与1相乘

[英]Performance function call vs multiplication by 1

Look at this function: 看一下这个功能:

float process(float in) {
  float out = in;

  for (int i = 0; i < 31; ++i) {
    if (biquads_[i]) {
      out = biquads_[i]->filter(out);
    }
  }

  return out;
}

biquads_ is a std::optional<Biquad>[31] . biquads_std::optional<Biquad>[31]

in this case i check for every optional to check if its not empty, and then call the filter function of biquad, if instead I unconditionally call filter function, changing it to multiply by 1 or simply return the input value, would be more efficient? 在这种情况下,我检查每个可选项以检查其是否不为空,然后调用biquad的filter函数,如果相反,我无条件调用filter函数,将其更改为乘以1或简单地返回输入值,会更有效吗?

Most likely it won't make a shread of difference (guessing somewhat though since your question is not entirely clear). 很可能不会带来任何改变(尽管由于您的问题尚不完全清楚,所以还是有所猜测)。 For two reasons: 1) unless the code is going to be used in a very hot path, it won't matter even if one way is a few nanoseconds faster than the other. 出于两个原因:1)除非代码会在非常热的路径中使用,否则即使一种方法比另一种方法快几纳秒也没关系。 2) most likely your compilers optimizer will be clever enough to generate code that performs close-to (if not identical to) the same in both cases. 2)最有可能的是,您的编译器优化器将足够聪明,可以生成在两种情况下都可以执行(即使不完全相同)的代码。 Did you test it? 你测试了吗? Did you benchmark/profile it? 您有基准测试/配置文件吗? If not; 如果不; do so - with optimization enabled. 这样做的- 优化功能。

Strive to write clear, readable, maintainable code. 努力编写清晰,可读,可维护的代码。 Worry about micro-optimization later when you actually have a problem and your profiler points to your function as a hot-spot. 当您实际遇到问题并且探查器指向您的热点功能时,稍后再担心微优化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM