Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

import package performance  RSS feed

 
Timothy Willard
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am in the process of finding ways to imporve the performance of an eCommerce Java Application. After looking at the code, I have noticed that many of the Java classes have unnecessary import statements. I assume that the performance gain of eliminating unwanted import statements in a class would be very minimal. However, I just wanted to get others opinions on this topic. Also, what would be the performance of importing a package directory versus an individual class file.
ie.
import java.util.*;
vs.
import java.util.Vector;
 
Mark Herschberg
Sheriff
Posts: 6037
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure of the performence implications, but I always use the specific import. It's much clearer what's being imported (oh, he's using a FileStream, not just doing some type IO) and makes the code easier to read (despite being bigger).
--Mark
 
John Bateman
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I also use the specific over the general (I.e> java.util.ArrayList over java.util.*), but, this is only for readability sake.
The imports are resolved at compile time and only classes needed are referenced/imported in the class file.
So if your class file has the following.

If you compile this class and run it, only the Vector class will be referenced/imported into the class file.
All the other imported classes will be ignore. The compiler only uses class that are actually instantiated / referenced by your program, not by what's defined as an import. Think of the import as an internal path to where to find these files, not as a sign to the compiler to implicitly add hem to the .class file.
Because all of this is done at compile time, you will have no performance gain/loss at runtime.
This, in the end means no performance diffrence when running in the JVM.

------------------
SOURCE CODE should be SURROUNDED by "code" tags.
Click here for an example
 
Junaid Bhatra
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John is absolutely correct. Importing a package as opposed to a particular class has no implications on run-time performance. Infact the bytecode that is generated does not even have any import directives. During compilation, the class names are replaced by the fully-qualified class names (which include the package names). Thus if you import a directory (instead of a class), the time required for compilation might be slightly longer, since the compiler has to search for your particular class in the directory. But it would in no way affect run-time performance
 
Timothy Willard
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to thank everyone for responding to my topic. I feel that I have learned alot about import statements from your replies.
 
Biju Philip NYC
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, if you want to get rid of import statements that are truly unnecessary, there are some IDEs have an option to do so. For example, the JDE for emacs has a LISP function (invoked by M-X JDE-import-kill-extra-imports) that will remove those import statements that are not pertintent to the class you are writing. But as stated above, these types of changes will not have any impact on runtime performance.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!