English 中文(简体)
什么是更快的? 职能要求或条件说明?
原标题:Which one is faster ? Function call or Conditional if Statement?

在回答这一问题之前,请考虑事务组的预测。

我有一些假想,在职能协调人的帮助下,我可以用要求履行职务来取代有条件的声明。 与此类似。 (你可以考虑以遗产方案为基础,为类似类型的sen)

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

也可以这样写。

       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();
        } 
     }

国际人权联合会认为,这两种情况都取得了相同的结果。 但我想到的是业绩管理。 在第二种情况下,Im通过职能电话避免出现分支预测问题。

现在让我知道,在这种意见中,哪些是更好的实践和更好的优化守则。

P.S.:我看不出有人详细概述哪怕是《集会法》中哪怕是“不好的分支预测”。

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.

最佳回答

如果发言是间接的,你将替代发言。

您的发言和间接的传承都需要获得记忆。

然而,如果会导致短暂的跳跃,可能赢得输油管的废气,而间接电线可能会使输油管失效。

另一方面,间接影响是跳跃的,而如果声明是有条件的跳跃的话。 分支机构预测者可能错失。

很难说,如果不测试,情况会更快。 我预测,如果发言将获胜的话。

请分享你们的成果。

问题回答

你们需要了解这种守则,以便能够就特定环境(比较、汇编版、非洲顾问办、硬件)作出某种说明,而且你需要在一个具体应用中衡量,以便了解这一应用是否具有任何意义。 除非你编写图书馆守则,否则,除非貌相显示这是你申请中的一个热点,否则,并非两者兼有。

仅写最易读的法典,最容易维持。 优化清洁、无泡沫和易于阅读的代码总是比确定优化的编码容易。

尽管如此,我记得Lippman在他的上。 C++ 标的 引证研究发现虚拟功能(基本功能点)至少与实际世界应用中的类型转换一样快。 我不知道细节,但书上有些地方。

It is more likely that the optimizer can apply his magic on the if statement, then on a dynamically changing function pointer. Just a guess, theoretically the compiler is allowed to do anything that he can prove does not change the semantics.

但就您而言,CPU更可能适用<>t/em> magic,即重订指令、预约等。 如果大多数邮联之间有职能,则很可能无法“缓冲”其管道,因此不可能实现CPU-optim化。

尽管如此,如果你把一切放在一个头脑、打电话和打电话的人身上,汇编者可能会放弃要求的职能,从而将重定自己。

衡量标准。 1M次。 C++11使用<chrono>,monothonic_clock:now()

Update: My experience is: Don t over-optimize your code my hand -- it is very likely that you make it worse. Let the compiler do the work, and for that make as much code visible to it as you can. If you do, you definitely need a profiler, try out alternatives, use the hints some of them offer you. But don t forget: This must be fine-tuned very carefully. Only one measurement of performance is speed. There is also readability, testability and reusebility and many more. And to quote Donald Knuth: "Premature optimization is the root of all evil."





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签