java调用接口速度慢,Java中接口调用的优化策略

I have an application that utilizes a Provider Pattern. The application utilizes the provider implementation by calling interfaces that are defined by the application.

I'm currently researching ways I can optimize my application around interface calls.

I can limit the complexity of my application to the following:

I only need to load the implementation dynamically once on start-up

I only need one provider implementation for a particular set of interfaces for an application instance at any one time.

I would appreciate any strategies people have put into practice to:

Reducing interface calls

Any tricks to generically call the interface implementation classes directly.

Various ways to take better advantage of any compiler optimizations.

Thank you!

解决方案

Some mis-conceptions worth addressing here.

You can reduce interface calls by calling the underlying concrete implementations directly (or using abstract classes) You should do this when it simplifies the design and improves maintainability (usually more interfaces helps, but not always)

You can cast an interface reference to a concrete reference and use that instead. This is generally bad programing practice and has little impact on performance. Generally you only do this when refactoring properly is more work than its worth. (Its not a good design approach but can be a pragmatic one)

The compiler doesn't optimize the code in any significant way. The few optimizations it does (such as inlining compile time constants) you could probably do without. The JVM does the useful optimizations at runtime. Second guessing what the JVM will do and try to optimise your code is a trail and error process. If you try 10 things which you think should help, 1 will make it significantly faster, 6 will make little difference and 3 will make it slower. i.e. simple code tends to be optimized best.

Unlike C++, most JVMs can optimise out the cost of virtual functions, esp when there is only one or two implementations actually used. i.e. it can optimise code based on how it is used rather than what the compiler can staticly determine.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值