Java中接口调用的优化策略 [英] Optimization Strategies around Interface Calls in Java

查看:629
本文介绍了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:


  1. 我只需要在启动时动态加载实现

  2. 我在任何时候都只需要为应用程序实例的一组特定接口实现一个提供程序实现。

我会很感激人们已经付诸实践的任何策略:

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


  1. 减少接口调用

  2. 一般来电的任何技巧直接接口实现类。

  3. 各种方法可以更好地利用任何编译器优化。

谢谢!

推荐答案

这里有一些错误的概念值得解决。

Some mis-conceptions worth addressing here.


  1. 您可以通过直接调用底层具体实现来减少接口调用(或使用抽象类)当你简化设计并提高可维护性时你应该这样做(通常更多接口有帮助,但并非总是如此)

  1. 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)

编译器不会以任何显着的方式优化代码。它可以做的少数优化(例如内联编译时常量),你可能没有。 JVM在运行时执行有用的优化。再猜测JVM将做什么并尝试优化代码是一个跟踪和错误过程。如果你尝试了10件你认为应该有帮助的东西,1会使它显着加快,6会有所不同,3会使它变慢。即简单代码往往最优化。

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.

与C ++不同,大多数JVM可以优化虚拟功能的成本,当实际使用的实现只有一两个时,esp。即它可以根据代码的使用方式优化代码,而不是编译器可以静态确定的代码。

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.

这篇关于Java中接口调用的优化策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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