about the cost of virtual function

前端 未结 7 1728
没有蜡笔的小新
没有蜡笔的小新 2021-01-11 23:41

If I call a virtual function 1000 times in a loop, will I suffer from the vtable lookup overhead 1000 times or only once?

7条回答
  •  失恋的感觉
    2021-01-12 00:16

    If the compiler can deduce that the object on which you're calling the virtual function doesn't change, then, in theory, it should be able to hoist the vtable lookup out of the loop.

    Whether your particular compiler actually does this is something you can only find out by looking at the assembly code it produces.

提交回复
热议问题