92

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?

SecretAgentMan
  • 2,856
  • 7
  • 21
  • 41
Dheeraj Joshi
  • 3,057
  • 8
  • 38
  • 55

6 Answers6

83

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.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 1
    We can also mention a few additional CPU ticks in the compilation process. More code - more time it needs to optimize and compile – disorder Jan 04 '12 at 08:46
  • Thanks for the answer! Do you have a source for this info that I can reference? (other than this answer...) – Eduardo Bezerra Sep 27 '16 at 16:24
  • 2
    @EduardoBezerra you can see from the byte cide specification that imports are not used so they can have an effect at runtime. – Peter Lawrey Sep 27 '16 at 19:02
31

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.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Frankline
  • 40,277
  • 8
  • 44
  • 75
  • 4
    Please provide reference for `unused imports have no performance impact` – RanRag Jan 04 '12 at 08:52
  • 3
    To be more precise, there is no performance impact at RUNTIME. However, depending on the number of imports, COMPILATION may be slower however unnoticeable. – Frankline Jan 12 '12 at 12:42
  • Why unnoticeable? Surely that depends on how many compilations you're doing? For example - we have a CI build server that is continually compiling multiple projects - therefore even the smallest optimization should increase compile performance slightly, right? – ryan Jun 05 '13 at 12:40
  • If something takes a day longer than usual, you'll notice it... assuming it's something that would normally take less than a few days. What if it normally took a year? You're not going to notice it because it is just noise at that magnitude. There are more rewarding things to focus on than a 0.274% performance increase. If you really need that little of an increase, throw better hardware at it or improve the compiler, you'll get far more out of that than going after pocket change. – Chinoto Vokro Dec 17 '16 at 08:33
  • @SantiBailors "one whole day" out of what? That day is spent by the computer, not you. A computer can't fix bugs (unless programmed to?), otherwise you wouldn't be needed. A change that gains 1/1000 inside a 1000 iteration loop is still just a 1/1000 gain, I don't see your point. If someone wants to pay you for such a small gain, that's great for you, otherwise it's a waste of time. – Chinoto Vokro Aug 06 '18 at 15:58
  • @ChinotoVokro But I don't get the _A change that gains 1/1000 inside a 1000 iteration loop is still just a 1/1000 gain_ If you gain 1/1000" on an operation which is not in a loop, and later that operation needs to be moved inside a 1000 iteration loop, the original optimization now causes a 1" gain and no longer a 1/1000" gain. This is my point (about the loop thing only; my considerations about time spent I do agree that were wrong and confusing). – SantiBailors Aug 07 '18 at 10:35
9

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.

montrealist
  • 5,593
  • 12
  • 46
  • 68
Ossama Boughaba
  • 191
  • 2
  • 5
1

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

Ouissal
  • 1,519
  • 2
  • 18
  • 36
0

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

Neeraj
  • 171
  • 2
  • 11
0

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[]...

Delark
  • 1,141
  • 2
  • 9
  • 15