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

前端 未结 15 1274
慢半拍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:09

    I'm reinforcing all answers that say in effect:

    • If you don't actually know it's a problem, any concern about fixing it is probably misplaced.

    What you want to know is:

    • What fraction of execution time (when it's actually running) is spent in the process of invoking methods, and in particular, which methods are the most costly (by this measure).

    Some profilers can give you this information indirectly. They need to summarize at the statement level, but exclusive of the time spent in the method itself.

    My favorite technique is to just pause it a number of times under a debugger.

    If the time spent in the process of virtual function invocations is significant, like say 20%, then on the average 1 out of 5 samples will show, at the bottom of the call stack, in the disassembly window, the instructions for following the virtual function pointer.

    If you don't actually see that, it is not a problem.

    In the process, you will probably see other things higher up the call stack, that actually are not needed and could save you a lot of time.

提交回复
热议问题