哪一个是更快?函数调用或有条件的if语句? [英] Which one is faster ? Function call or Conditional if Statement?

查看:135
本文介绍了哪一个是更快?函数调用或有条件的if语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请过回答这个问题之前,需要考虑的分支prediction。

Please consider the branch prediction too before answering this question.

我有一些场景在那里我可以用功能pointer.Some这样的事帮助一个函数的调用替换条件语句。 (你可以认为基于组件的编程在继承相似类型的塞纳里奥)

I have some scenarios where i can replace a conditional statement with a call to a function with the help of function pointer.Some thing like this. (you can think of component based programming over inheritance for a similar type of senario)

     class Shape
     {
        float Area()
        {
            if(type == SQUARE)
             {
                return length*length;
             }
            else if(type == RECTANGLE)
            {
             return length*breadth;
            }
        } 
     }

同一个类可以这样写。

The same class can be written like this.

       class Shape
     {
        void SetAreaFunction(void *funcptr)//this function is used to set the current AreaFunc
        {
             CurrentAreaFunc = funcptr ;//this holds the pointer to current area func
        }
        float SqauareArea();//this will return square area
        float RectangleArea();//this will return rectangle area 
        float Area()
        {
            currentAreaFunc();
        } 
     }

如果你考虑到上述情况,既达到相同results.But,我在想overhead.In我避免了有一个函数调用有分支prediction问题第二种情况下的性能。

IF you consider the above cases, both achieves same results.But, I'm thinking about the performance overhead.In the second case I'm avoiding having branch prediction problem by having a function call.

现在让我知道这是更好的做法,并在这种senarios的更好的优化code'。(顺便说一句,我不喜欢的声明pre-成熟的优化是万恶之源作为,优化有它的好处,所以我认为我的优化code!)

Now let me know which is the better practice and 'better optimized code' in this kind of senarios.(btw, I don't like the statement "Pre-mature optimization is root of all evil" as, optimization has its benefits so i do consider optimizing my code!)

P.S:如果任何人提供有关怎么不好分支prediction可以,即使在组装code的详细介绍,我不介意

P.S: I don't mind if any one give a detailed overview about 'how bad branch prediction can be" even in assembly code.

更新:分析(一种类似上述code的),结果后
     如果条件成功地将这种senario.Can中的任何一个给出这方面的原因?
功能调用code可能是prefetched因为没有分支code吧?但在这里它看起来其他way..branching code胜! :o
异形英特尔MAC OSX,GCC O3 / O的优化。

Update: After profiling (similar kind of above code),
If Condition succeeded in this kind of senario.Can any one give a reason for this? Functional call code can be prefetched as there is no Branching code right? But here its looks the other way..branching code wins! :O Profiled on Intel Mac Osx,GCC O3/Os optimisation.

推荐答案

您更换if语句与间接。

You replaced an if statement with an indirection.

您都if语句,而需要间接内存访问。

Both your if statement, and the indirection requires memory access.

然而,如果将导致短跳转 - 这将可能不会失效管道,而间接可能违反管道

However, the if will result in a short jump - which will probably won't invalidate the pipeline, while the indirection may invalidate the pipeline.

在另一方面,在间接是跳跃,而如果语句是一个条件跳转。分支predictor可能会错过。

On the other hand, the indirection is a jump, while the if statement is a conditional jump. The branch predictor may miss.

这是很难说这是没有测试它更快。我predict的if语句将获胜。

It is hard to tell which is faster without testing it. I predict that the if statement will win.

请分享你的成果!

这篇关于哪一个是更快?函数调用或有条件的if语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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