简体   繁体   English

Java导入与代码性能

[英]Java import vs code performance

I am wondering if i included many import in my java program, would it affect the performance of my code (for example, program will be slower)? 我想知道我的Java程序中是否包含许多import ,这会影响代码的性能吗(例如,程序会变慢)? Is the logic behind the import in Java the same as include in C? Java中import背后的逻辑与C中include的逻辑相同吗?

would it affect the performance of my code (for example, program will be slower)? 它会影响我的代码的性能吗(例如,程序会更慢)?

No, it wouldn't affect performance of your code. 不,这不会影响代码的性能。

The binaries (the class files) do not increase in size as the import is not implemented with any cut-and-paste mechanism. 二进制文件(类文件)的大小不会增加,因为导入没有使用任何剪切和粘贴机制来实现。

It is merely a syntactic sugar for avoiding to have to write for instance 例如,它只是避免必须写的语法糖

java.util.List<java.math.BigInteger> myList =
        new java.util.ArrayList<java.math.BigInteger>();

Here is a little test demonstrating this: 这是一个小测试,证明了这一点:

aioobe@e6510:~/tmp$ cat Test.java 
import java.util.*;

public class Test {
    public static void main(String[] args) {
        List<Integer> myInts = new ArrayList<Integer>();
    }
}
aioobe@e6510:~/tmp$ javac Test.java
aioobe@e6510:~/tmp$ md5sum Test.class 
523036e294b17377b4078ea1cb8e7940  Test.class

(modifying Test.java ) (修改Test.java

aioobe@e6510:~/tmp$ cat Test.java 


public class Test {
    public static void main(String[] args) {
        java.util.List<Integer> myInts = new java.util.ArrayList<Integer>();
    }
}
aioobe@e6510:~/tmp$ javac Test.java
aioobe@e6510:~/tmp$ md5sum Test.class 
523036e294b17377b4078ea1cb8e7940  Test.class

Is the logic behind the import in Java the same as include in C? Java中导入背后的逻辑与C中包含的逻辑相同吗?

No, an #include is a preprocessor directive and is implemented with a cut-and-paste mechanism. 不, #include是预处理程序指令,并通过剪切和粘贴机制实现。

... would it affect the performance of my code ...是否会影响我的代码的性能

Not in the slightest. 没有丝毫。 In fact, the compiled classes (using imports or not) will be identical. 实际上,已编译的类(是否使用导入)将是相同的。 An import is merely syntactic sugar that allows you to use a shorter name for an external class or (with a static import) class member in your source code. 导入只是语法糖,它允许您在源代码中为外部类或(带有静态导入)类成员使用较短的名称。 In other words, it allows you to write: 换句话说,它允许您编写:

    Map map = new HashMap();

instead of 代替

    java.util.Map map = new java.util.HashMap();

That is all. 就这些。

There is potentially a small (tiny) difference in compilation times. 编译时间可能有微小的差异。 But, AFAIK, something like import java.util.*; 但是,AFAIK类似于import java.util.*; ;。 does NOT cause all of the java.util classes to be loaded by the compiler. 不会导致所有java.util类都由编译器加载。 Rather it just adds the names of the classes to the symbol table. 而是将类的名称添加到符号表中。

Having said that: 话说回来:

  • Unnecessary imports are a bad idea, because they clutter up the code and could mislead someone reading the code. 不必要的导入是个坏主意,因为它们会使代码混乱,并可能误导阅读代码的人。
  • Wildcard imports ( .* ) can lead to unexpected collisions. 通配符导入( .* )可能导致意外冲突。
  • A lot of people (myself included) dislike wildcard imports because they prefer to see a list of the actual classes used. 许多人(包括我自己)不喜欢通配符的导入,因为他们更喜欢看到所使用的实际类的列表。

Is the logic behind the import in Java the same as include in C? Java中导入背后的逻辑与C中包含的逻辑相同吗?

No it is not. 不它不是。

AC / C++ include directive injects arbitrary 1 C / C++ "code" into the source stream. AC / C ++ include指令将任意1 C / C ++“代码”注入源流。 This can include declarations and executable statements ... which can affect both performance, execution memory footprint and the size of the executable. 这可能包括声明和可执行语句……这可能会影响性能,执行内存占用量和可执行文件的大小。


1 - That is, whatever the authors of the include file chose to put into the file. 1-也就是说,无论include文件的作者是选择放入文件中。 It could be simple method and class "signatures", but it could also be macro's, code and other declarations that are impactful. 它可能是简单的方法和类“签名”,但也可能是影响有效的宏,代码和其他声明。 You have to examine the file to be sure. 您必须检查文件以确保。

It will have no impact on the ~run~time speed of your program. 这不会影响程序的运行时间

It may have an impact on the ~compile~time speed of your program. 它可能会影响程序的〜compile_time速度

If you import java.util.*; 如果import java.util.*; it will load all of the java.util package into the compiler, which may increase the compile time when you .* an entire package for a single usage (although you should perform some profiling if it's going to be a concern.) 它将所有java.util程序包加载到编译器中,这可能会增加.*单个用法的整个程序包的编译时间(尽管如果有问题,您应该执行一些性能分析)。

In addition to potential compile time issues, don't forget to consider the readability issues. 除了潜在的编译时问题外,别忘了考虑可读性问题。 Generally, I (and people I've talked with) find import pack.age.Class; 通常,我(和与我交谈过的人)找到import pack.age.Class; to be more readable than import pack.age.*; import pack.age.*;更易读 - have a talk with your team of course before making a decision about this. -在就此做出决定之前,先与您的团队讨论。

But the logic behind it is very different from #include and does not bloat the code. 但是其背后的逻辑与#include完全不同,并且不会膨胀代码。 You may end up with more than necessary as you include dependency jars, but that's probably not a big issue. 当您包含依赖项jar时,最终可能会超出所需的范围,但这可能不是一个大问题。

import does not slow your program. import不会降低您的程序速度。 It is better to have different kinds of classes in different packages and import them as needed. 最好在不同的packages包含不同种类的classes ,然后根据需要导入它们。 Improves code readability. 提高代码的可读性。

Importing is not an issue. 导入不是问题。 You can import everything you want from package and execution of your code will not get slower. 您可以从包中导入所需的所有内容,并且代码的执行不会变慢。 import is for convenience so you can use shorter names. 导入是为了方便起见,因此可以使用较短的名称。

The classes are loaded when their constructor is called implicitly or explicitly ( new operator). 当隐式或显式调用它们的构造函数( 运算符)时,将加载类。 In case of enums it will be when you refer to enum names in your code. 对于枚举,将在代码中引用枚举名称。 This will cause class (enum) to be loaded. 这将导致类(枚举)被加载。 (You can try using println in private constructor of enum to experiment and see when enum gets loaded). (您可以尝试在枚举的私有构造函数中使用println进行实验,并查看何时加载枚举)。 Loading a class takes time and memory. 加载课程需要时间和记忆。 Generally speaking loaded classes are not meant to be unloaded. 一般来说,已加载的类并不意味着要被卸载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM