問題描述
在我的類設計中,我廣泛使用抽象類和虛函數.我有一種感覺,虛函數會影響性能.這是真的?但我認為這種性能差異并不明顯,看起來我正在做過早的優化.對嗎?
In my class design, I use abstract classes and virtual functions extensively. I had a feeling that virtual functions affects the performance. Is this true? But I think this performance difference is not noticeable and looks like I am doing premature optimization. Right?
推薦答案
一個好的經驗法則是:
除非你能證明它,否則這不是性能問題.
It's not a performance problem until you can prove it.
使用虛函數會對性能產生非常輕微的影響,但不太可能影響應用程序的整體性能.尋找性能改進的更好的地方是算法和 I/O.
The use of virtual functions will have a very slight effect on performance, but it's unlikely to affect the overall performance of your application. Better places to look for performance improvements are in algorithms and I/O.
一篇關于虛函數(以及更多)的優秀文章是成員函數指針和最快的 C++ 委托.
An excellent article that talks about virtual functions (and more) is Member Function Pointers and the Fastest Possible C++ Delegates.
這篇關于虛函數和性能 - C++的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!