在 C++ 中使用接口的性能损失? [英] Performance penalty for working with interfaces in C++?

查看:32
本文介绍了在 C++ 中使用接口的性能损失?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 C++ 中使用接口(抽象基类)时是否有运行时性能损失?

Is there a runtime performance penalty when using interfaces (abstract base classes) in C++?

推荐答案

简短回答:否

长答案:影响速度的不是基类或类在其层次结构中的祖先数量.唯一的问题是方法调用的成本.

Long Answer: It is not the base class or the number of ancestors a class has in its hierarchy that affects it speed. The only thing is the cost of a method call.

非虚方法调用有成本(但可以内联)
虚拟方法调用的成本稍高,因为您需要在调用之前查找要调用的方法(但这是一个简单的表查找不是搜索).由于接口上的所有方法根据定义都是虚拟的,因此存在此成本.

A non virtual method call has a cost (but can be inlined)
A virtual method call has a slightly higher cost as you need to look up the method to call before you call it (but this is a simple table look up not a search). Since all methods on an interface are virtual by definition there is this cost.

除非您正在编写一些对超速敏感的应用程序,否则这应该不是问题.您从使用界面中获得的额外清晰度通常可以弥补任何感知到的速度下降.

Unless you are writing some hyper speed sensitive application this should not be a problem. The extra clarity that you will recieve from using an interface usually makes up for any perceived speed decrease.

这篇关于在 C++ 中使用接口的性能损失?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆