实现接口的开销 [英] Overhead of implementing an interface

查看:144
本文介绍了实现接口的开销的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的一个同事告诉我,实现接口引入了开销。这是真的吗?

One of my colleagues told me that implementing interfaces introduces overhead. Is this true?

我不关心微优化;我只是想知道的更深层次的细节这需要。

I am not concerned about micro optimizations; I just want to know the deeper details this entails.

推荐答案

忍不住和测试,它看起来几乎没有任何开销。

couldn't resist and tested it and it looks like almost no overhead.

参与者是:

Interface IFoo    defining a method
class Foo: IFoo   implements IFoo
class Bar         implements the same method as Foo, but no interface involved

所以我定义的

so i defined

Foo realfoo = new Foo();
IFoo ifoo = new Foo();
Bar bar =  new Bar();

和调用的方法,它确实对每个变量20字符串连接,1000万次。

and called the method, which does 20 string concatenations, 10,000,000 times on each variable.

realfoo:   723 Milliseconds
ifoo:      732 Milliseconds
bar:       728 Milliseconds

如果该方法不执行任何操作,在实际通话中脱颖而出多一点。

If the method does nothing, the actual calls stand out a bit more.

  realfoo: 48 Milliseconds
  ifoo: 62 Milliseconds
  bar: 49 Milliseconds

这篇关于实现接口的开销的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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