Java 导入与代码性能
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5125404/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java import vs code performance
提问by newbie
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)? Is the logic behind the import
in Java the same as include
in C?
我想知道如果我import
在我的 java 程序中包含了很多,它会影响我的代码的性能吗(例如,程序会变慢)?import
Java 中的背后的逻辑与include
C 中的相同吗?
回答by aioobe
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 notimplemented 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.
不, an#include
是一个预处理器指令,是通过剪切和粘贴机制实现的。
回答by Stephen C
... 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.*;
does NOT cause all of the java.util
classes to be loaded by the compiler. Rather it just adds the namesof the classes to the symbol table.
编译时间可能存在微小(微小)差异。但是,AFAIK,类似的事情import java.util.*;
不会导致java.util
编译器加载所有类。相反,它只是将类的名称添加到符号表中。
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.
不它不是。
A C / C++ include directive injects arbitrary1C / C++ "code" into the source stream. This can include declarations and executable statements ... which can affect both performance, execution memory footprint and the size of the executable.
AC / C++ 包含指令将任意1C / C++ “代码”注入源流。这可能包括声明和可执行语句……它们会影响性能、执行内存占用和可执行文件的大小。
1 - That is, whatever the authors of the include file chose to put into the file. 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.
1 - 也就是说,无论包含文件的作者选择放入文件中。它可以是简单的方法和类“签名”,但也可以是宏、代码和其他有影响的声明。您必须检查文件才能确定。
回答by corsiKa
It will have no impact on the ~run~time speedof your program.
它不会影响程序的~run~time 速度。
It may have an impact on the ~compile~time speedof your program.
它可能会影响程序的~compile~time 速度。
If you 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.)
如果您import java.util.*;
将所有 java.util 包加载到编译器中,当您.*
将整个包用于单次使用时,这可能会增加编译时间(尽管您应该执行一些分析,如果它会成为一个问题。)
In addition to potentialcompile time issues, don't forget to consider the readability issues. Generally, I (and people I've talked with) find import pack.age.Class;
to be more readable than import pack.age.*;
- have a talk with your team of course before making a decision about this.
除了潜在的编译时问题,不要忘记考虑可读性问题。一般来说,我(以及我与之交谈过的人)发现import pack.age.Class;
比import pack.age.*;
- 当然在做出决定之前与您的团队交谈更具可读性。
But the logic behind it is very different from #include
and does not bloat the code. You may end up with more than necessary as you include dependency jars, but that's probably not a big issue.
但是其背后的逻辑#include
与代码非常不同并且不会膨胀。当您包含依赖 jar 时,您最终可能会遇到不必要的问题,但这可能不是什么大问题。
回答by fastcodejava
import
does not slow your program. It is better to have different kinds of classes
in different packages
and import them as needed. Improves code readability.
import
不会减慢你的程序。最好classes
在不同的地方有不同的种类packages
并根据需要导入它们。提高代码可读性。
回答by Witold Kaczurba
Importing is not an issue. You can import everything you want from package and execution of your code will not get slower. importis for convenience so you can use shorter names.
进口不是问题。您可以从包中导入您想要的所有内容,并且代码的执行不会变慢。import是为了方便起见,因此您可以使用较短的名称。
The classes are loaded when their constructor is called implicitly or explicitly (newoperator). 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). Loading a class takes time and memory. Generally speaking loaded classes are not meant to be unloaded.
类在其构造函数被隐式或显式调用(新运算符)时加载。在枚举的情况下,当您在代码中引用枚举名称时。这将导致加载类(枚举)。(您可以尝试在 enum 的私有构造函数中使用 println 进行试验并查看 enum 何时加载)。加载一个类需要时间和内存。一般来说,加载的类并不意味着要卸载。