AI Applications in C++: How costly are virtual functions? What are the possible optimizations?

前端 未结 15 1215
慢半拍i
慢半拍i 2020-12-23 12:43

In an AI application I am writing in C++,

  1. there is not much numerical computation
  2. there are lot of structures for which run-time polymorphism is ne
15条回答
  •  礼貌的吻别
    2020-12-23 13:27

    As already stated by the other answers, the actual overhead of a virtual function call is fairly small. It may make a difference in a tight loop where it is called millions of times per second, but it's rarely a big deal.

    However, it may still have a bigger impact in that it's harder for the compiler to optimize. It can't inline the function call, because it doesn't know at compile-time which function will be called. That also makes some global optimizations harder. And how much performance does this cost you? It depends. It is usually nothing to worry about, but there are cases where it may mean a significant performance hit.

    And of course it also depends on the CPU architecture. On some, it can become quite expensive.

    But it's worth keeping in mind that any kind of runtime polymorphism carries more or less the same overhead. Implementing the same functionality via switch statements or similar, to select between a number of possible functions may not be cheaper.

    The only reliable way to optimize this would be if you could move some of the work to compile-time. If it is possible to implement part of it as static polymorphism, some speedup may be possible.

    But first, make sure you have a problem. Is the code actually too slow to be acceptable? Second, find out what makes it slow through a profiler. And third, fix it.

提交回复
热议问题