未使用的导入和对象会对性能产生影响吗?

发布于 2024-12-24 03:54:33 字数 88 浏览 5 评论 0原文

Java 代码中未使用的导入和未使用的对象是否会对性能产生任何影响?

假设一个对象被初始化但从未使用过,会发生什么? 未使用的进口产品的成本是多少?

Do the unused imports and unused objects in Java code create any performance impact?

Suppose an object is initialized and never used, what happens?
And what is the cost of unused imports?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

苍白女子 2024-12-31 03:54:33

这是一个非常常见的问题。

与大多数性能问题一样,最好的方法是尽可能编写最清晰、最简单的代码,因为这可以提高代码的可维护性,并有助于确保即使在更改后也能表现良好。 (聪明/迟钝/不必要的冗长代码一开始可以运行得很快,但是当它被普通人更改时,它会变得慢得多)

未使用的导入对编译器有微不足道的影响,但在字节代码或运行时没有导入。

可以优化掉未使用的对象,但最好避免这些,因为它们几乎总是会造成一些性能影响,但更重要的是使阅读和维护代码变得更加困难。

Its a very common question.

Like most performance questions the best approach is to write the clearest and simplest code you can as this improves the maintainability of the code and helps ensure it performs reasonably well even after it is changed. (Clever/Obtuse/Needlessly Verbose code can run fast to start with but as it is changed by mere mortals it can get much slower)

Unused imports have a trivial impact on the compiler, but there are no imports in the byte code or at runtime.

Unused objects can be optimised away, but its best to avoid these as they almost always cause some performance impact, but more importantly make reading and maintaining your code more difficult.

夏夜暖风 2024-12-31 03:54:33

未使用的导入在运行时不会影响性能。它纯粹是一种命名空间机制。尽管如此,您应该始终只导入可读性所需的内容,并避免令人讨厌的命名空间冲突。

除了代码的可读性和代码的可维护性之外,通过整理导入可能会更快地编译 java 代码(但是,不明显),但运行时性能不会受到影响,因为生成的字节代码不会受到不整洁的导入的影响。生成的字节码保持不变。

Unused imports have no performance impact at runtime. It is purely a namespace mechanism. Nonetheless, you should always import only what you need for readability and avoid namespace collisions which are a nuisance.

Apart from code readability and hence maintainability of code, there may be faster compilation of java code (however, unnoticeable) by tidying up imports, but runtime performance is not impacted, since byte code generated is not impacted by untidy imports. Byte code generated remains the same.

虽然对编译的影响很小,但对部署的影响可能很糟糕。我刚刚遇到一个未使用的导入,它需要一个单独的库,该库成为 Maven 依赖项。幸运的是没有发现进一步的传递依赖问题,但是 .war 文件无缘无故变厚了。在 webapp 类加载器中添加一个多余的 jar。

While impact in compilation is minimal, the impact in deployment can be bad. I've just come across an unused import that required a separate library which became a maven dependency. A further transitive dependency problem was fortunately not found, but the .war file was thicker for no reason. Add to that a superfluous jar in the webapp classloader.

清晨说晚安 2024-12-31 03:54:33

尽管 Java 文件中未使用的导入不会造成任何损害,但它会不必要地增加 Java 源文件的长度和大小。

Though unused imports in Java file do not create any harm, it unnecessarily increases the length and size of the Java source file.

愁以何悠 2024-12-31 03:54:33

是的,如果我们在 java 类中引用未使用的 import 语句,它会对性能产生一些影响。 Java 编译器将检查 import 语句中提到的引用,并在分钟级别上检查它对类性能的影响。

谢谢

Yes it impact a bit on performance, if we are referring unused import statement in our java class. The Java compiler will check for references mentioned into the import statement and at minute level it impact on the performance of the your class.

Thanks

霊感 2024-12-31 03:54:33

我认为这是一个常见问题,是任何编程语言固有问题的结果。

语法不允许精确解释机器正在做什么。

任何系统都由两个方面组成:“真实”和“蓝图”。

在“抽象”/“蓝图”的函数中进行编码是非常常见的。

import Database;
class MyPojo {

   int intField;

   public static class Manager {

      final MyPojo instance;

      public Manager(Database db) {
         instance = db.getMyPojo();
      }

   }

}

这将允许轻松找到任何与 MyPojo 相关的功能。
所以我不知道学术界如何定义两者之间的区别,但是任何“真实”的东西都涉及内存分配、引用/指针操作、竞争条件……等等……

系统的这两个视角是完全不同的,但是其中两个在同一个句法二维平面中表达……单词。

这对他们俩都不公平,建筑蓝图需要二维,但真实的必须在现场处理。

同样,仅使用 2 维语法处理复杂系统变得越来越困难,即使 IDE 尝试帮助我们处理超链接,它也成为一个可以在 3 维平面中轻松处理的问题。

我相信问题在于该语言如何从纯粹的 OOP 范式演变为功能反应范式,其中不变性现在允许定义“核心”数据类型......也许我们一直需要的只是数组[]......

I think this being a common question is a consequence of the inherent problem of any programming language.

Syntax DOES NOT allow for a precise interpretation of what the machine is doing.

Any system is composed of 2 sides: the "real" and the "blueprint".

And it is extremely common to code in function of the "abstract"/"blueprint".

import Database;
class MyPojo {

   int intField;

   public static class Manager {

      final MyPojo instance;

      public Manager(Database db) {
         instance = db.getMyPojo();
      }

   }

}

This will allow to easily find any MyPojo related functionality.
So I don't know how academia defines a distinction between both but, anything "real", involves memory allocation, reference/pointer manipulation, race conditions... etc...

These two perspectives of a system are completely different, yet the two of them are expressed in the same syntactical 2-dimensional plane... Words.

And it is not fair to either of them, a blueprint in architecture requires 2 dimensions, but the real must be handled on site.

The same way it becomes increasingly difficult to handle complex systems with just a 2 dimensional syntax, that even if IDE's try to help us with hyperlinks, it becomes an issue that could be handled easily in a 3 dimensional plane.

I believe the problem relies in how the language evolved from a pure OOP paradigm to a functional reactive one, where immutability now allows the defining of "nuclear" datatypes... Maybe all we always needed were arrays[]...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文