59

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?

0

5 Answers 5

84

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)

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?

No, an #include is a preprocessor directive and is implemented with a cut-and-paste mechanism.

Sign up to request clarification or add additional context in comments.

8 Comments

Not going to -1, but it can be more than syntactic sugar.
Like what? My understanding is that with import vs without import would look differently only up until the IR in the compiler. (Check out my example in the updated answer.)
That's essentially what I was busy answering. It can increase the compile time of your code under certain circumstances (namely .*)
@pst, an explanation what constant pool is would have suffice too
@bestsss It would be a nice addition for a more holistic answer, but it's hard argue with "bit-identical output".
|
24

... 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 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?

No it is not.

A C / C++ include directive injects arbitrary1 C / 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.


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.

Comments

16

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.

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

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; to be more readable than 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. You may end up with more than necessary as you include dependency jars, but that's probably not a big issue.

17 Comments

Trying to keep compile time speed in mind when writing code seems like a Bad Idea. You go benchmark this, find out if there is any significant difference and come back with your results :-)
For the speeds? Yes I do. javaperformancetuning.com/news/qotm031.shtml For the synchronized? There's one extra bytecode in the second one because there's a synchronized instruction, where as on the first one it's baked into the method modifiers (which in theory could make or break an inline/JIT, but incredibly unlikely).
Still no benchmark tests though. That would be interesting to have.
"can greatly increase the compile time" is an assertion of fact. By using "greatly", it also makes a claim of magnitude. If you wanted to present it as something to investigate, and make no claim on magnitude, you should have written "might affect compile time" instead. The assertion is given without sufficient evidence, and likely to be false. Factual inaccuracy is about the only reason I downvote answers, because a misleading answer is worse than none.
As for Pascal not getting a downvote, that's because he merely claims: "is probably going to take (a bit) longer for the compiler". "greatly" is more than "a bit", and "can" more than "will probably". And by the way, if you were aware of that duplicate question, why did you not link to it?
|
2

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.

2 Comments

what do you mean by "better to have different kinds of classes in different packages" thanks
@newbie I meant it is a good design to have different classes in different packages and import them as necessary. Probably it is not directly related to your question.
1

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). Loading a class takes time and memory. Generally speaking loaded classes are not meant to be unloaded.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.