Does unused import and objects have an performance

2019-01-07 15:39发布

I have a doubt, whether the unused imports and unused objects in Java code creates any performance impact?

Suppose an object is initialized and never used, what happens? And what is the cost of unused imports

5条回答
Root(大扎)
2楼-- · 2019-01-07 16:02

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.

查看更多
Rolldiameter
3楼-- · 2019-01-07 16:04

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.

查看更多
smile是对你的礼貌
4楼-- · 2019-01-07 16:21

While impact in compilation is minimal, the impact in deployment can be bad, I've just came across an unused import that required a separate library witch became a maven dependency, a further transitive dependency problem was not found hopefully but the war was thicker for no reason, add to that a superfluous jar in the webapp classloader.

查看更多
聊天终结者
5楼-- · 2019-01-07 16:23

Though unused import in Java file does not create any harm, it's unnecessary increase length and size of Java source file.

查看更多
三岁会撩人
6楼-- · 2019-01-07 16:25

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

查看更多
登录 后发表回答