Performance penalty for working with interfaces in C++?

前端 未结 16 1746
隐瞒了意图╮
隐瞒了意图╮ 2020-12-02 11:21

Is there a runtime performance penalty when using interfaces (abstract base classes) in C++?

相关标签:
16条回答
  • 2020-12-02 11:46

    One thing that should be noted is that virtual function call cost can vary from one platform to another. On consoles they may be more noticeable, as usually vtable call means a cache miss and can screw branch prediction.

    0 讨论(0)
  • 2020-12-02 11:46

    I know it's an uncommon viewpoint, but even mentioning this issue makes me suspect you're putting way too much thought into the class structure. I've seen many systems that had way too many "levels of abstraction", and that alone made them prone to severe performance problems, not due the cost of method calls, but due to the tendency to make unnecessary calls. If this happens over multiple levels, it's a killer. take a look

    0 讨论(0)
  • 2020-12-02 11:48

    Note that multiple inheritance bloats the object instance with multiple vtable pointers. With G++ on x86, if your class has a virtual method and no base class, you have one pointer to vtable. If you have one base class with virtual methods, you still have one pointer to vtable. If you have two base classes with virtual methods, you have two vtable pointers on each instance.

    Thus, with multiple inheritance (which is what implementing interfaces in C++ is), you pay base classes times pointer size in the object instance size. The increase in memory footprint may have indirect performance implications.

    0 讨论(0)
  • 2020-12-02 11:53

    Another alternative that is applicable in some cases is compile-time polymorphism with templates. It is useful, for example, when you want to make an implementation choice at the beginning of the program, and then use it for the duration of the execution. An example with runtime polymorphism

    class AbstractAlgo
    {
        virtual int func();
    };
    
    class Algo1 : public AbstractAlgo
    {
        virtual int func();
    };
    
    class Algo2 : public AbstractAlgo
    {
        virtual int func();
    };
    
    void compute(AbstractAlgo* algo)
    {
          // Use algo many times, paying virtual function cost each time
    
    }   
    
    int main()
    {
        int which;
         AbstractAlgo* algo;
    
        // read which from config file
        if (which == 1)
           algo = new Algo1();
        else
           algo = new Algo2();
        compute(algo);
    }
    

    The same using compile time polymorphism

    class Algo1
    {
        int func();
    };
    
    class Algo2
    {
        int func();
    };
    
    
    template<class ALGO>  void compute()
    {
        ALGO algo;
          // Use algo many times.  No virtual function cost, and func() may be inlined.
    }   
    
    int main()
    {
        int which;
        // read which from config file
        if (which == 1)
           compute<Algo1>();
        else
           compute<Algo2>();
    }
    
    0 讨论(0)
  • 2020-12-02 11:55

    There is a small penalty per virtual function call compared to a regular call. You are unlikely to observe a difference unless you are doing hundreds of thousands of calls per second, and the price is often worth paying for added code clarity anyway.

    0 讨论(0)
  • 2020-12-02 11:55

    As for any class that contains a virtual function, a vtable is used. Obviously, invoking a method through a dispatching mechanism like a vtable is slower than a direct call, but in most cases you can live with that.

    0 讨论(0)
提交回复
热议问题