Can repeated calls to the same function provide better performance?

I have code that looks like this: void foo(unsigned long k) { if (k & 1) { bar(k/2 + 1); bar(k/2); bar(k/2 + 1); } else { bar(k/2); bar(k/2); bar(k/2); } } void bar(unsigned long k) { switch(k) { case default: special_default(); break; case 1: specialbar1(); break; case 2: specialbar2(); break; case 16: specialbar16(); break; } } The performance is much better when `foo` is called for an even value of `k`. Each of the `specialbar#()` methods uses several stack variables, the number of such variables increases sharply as `k` increases. To be clear `specialbar#()` makes use of about `3 * k` local variables all of which are `unsigned long long` variables. For example `foo(32)` executes about 15% faster than `foo(31)`. I am using Visual Studio 2012 and the performance analysis assures me that two calls to `specialbar16` and one call to `specialbar15` takes considerably more work than three consecutive calls to `specialbar16`. Is it possible that the compiler takes advantage of the three consecutive calls when `k` is even? That is, can it realize that the stack is essentially the same over the three consecutive calls for even `k` yet the same optimization is not possible for odd `k`?
You aren't really measuring the same thing because you are calling different specialbar_k() functions in each case, and they presumably do different things.

以上就是Can repeated calls to the same function provide better performance?的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » CSS3 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏