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

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

问题描述

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

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

推荐答案

Long答案:
它不是基类或类的祖先数在其层次结构中影响其速度。唯一的是方法调用的成本。

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天全站免登陆