实现接口的开销开销、接口

由网友(总有宦官想害朕)分享简介:我的一个同事告诉我,实现接口引入了开销。这是真的吗?One of my colleagues told me that implementing interfaces introduces overhead. Is this true?我不关心微优化;我只是想知道的更深层次的细节这需要。I am not conc...

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

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
阅读全文

相关推荐

最新文章