Virtual Functions and Performance C++

后端 未结 8 668
梦毁少年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:36

    There could be several reasons for the difference in time.

    • your timing function isn't precise enough
    • the heap manager may influence the result, because sizeof(VCS) > sizeof(VS). What happens if you move the new / delete out of the loop?

    • Again, due to size differences, memory cache may indeed be part of the difference in time.

    BUT: you should really compare similar functionality. When using virtual functions, you do so for a reason, which is calling a different member function dependent on the object's identity. If you need this functionality, and don't want to use virtual functions, you would have to implement it manually, be it using a function table or even a switch statement. This comes at a cost, too, and that's what you should compare against virtual functions.

提交回复
热议问题