Virtual Functions and Performance C++

后端 未结 8 572
梦毁少年i
梦毁少年i 2021-01-18 04:10

Before you cringe at the duplicate title, the other question wasn\'t suited to what I ask here (IMO). So.

I am really wanting to use virtual functions in my applicat

8条回答
  •  生来不讨喜
    2021-01-18 04:32

    I think that this kind of testing is pretty useless, in fact:
    1) you are wasting time for profiling itself invoking gettimeofday();
    2) you are not really testing virtual functions, and IMHO this is the worst thing.

    Why? Because you use virtual functions to avoid writing things such as:

    
    switch typeof(object) {
    
    case ClassA: functionA(object);
    
    case ClassB: functionB(object);
    
    case ClassC: functionC(object);
    }
    
    

    in this code, you miss the "if... else" block so you don't really get the advantage of virtual functions. This is a scenario where they are always "loser" against non-virtual.

    To do a proper profiling, I think you should add something like the code I've posted.

提交回复
热议问题