Do unused use statements decrease performance?
Asked Answered
Q

2

20

I want to know if unused use statements in my class affect performance of my php website?

Does php include all classes in beginning or when need it? If second choice then I think it doesn't affect performance of my system.

For Example: Use statement 'DbConnector' is not used

use model\adapter\DbConnector;
Quiver answered 1/12, 2014 at 11:40 Comment(4)
All questions like these have the same answer: profile it.Welcome
If you are not using it, than why to keep it? It only makes mess in your code.Collide
I am alredy removing unused uses and I wonder if it will increase performance.Quiver
There's no valid reason to be sloppy, but it won't impact performance in any meaningful way.Flypaper
O
36

No, the use statement does not provoke the the class be loaded (it does not even trigger an autoloader).

It just declares a short name for a class. I assume the cost in terms of CPU and RAM is in the order of a few CPU cycles and a few bytes.

Oceangoing answered 1/12, 2014 at 11:58 Comment(4)
There is a scenario I am fighting, where because of too many use aliases and require_once calls, the opcode cache gives up resulting in a huge performance penalty. Without use aliasing but keeping all require_once calls, it all works fine. On PHP 7.0.x, didn't test others.Ridley
Also, this benchmark says fully qualified is always faster: veewee.github.io/blog/…Ridley
Feel free to add your own aswer, however I am under the impression that you have misunderstood the point of the question. We are discussing the potential cost of unused use clauses, not the cost of actually used use clauses.Oceangoing
I don't have an answer to make, I have a comment. Also, I'm a long time user, I guess I should've known by now! I didn't say the statements are actually used.Ridley
G
0

Newer versions of PHP, PHP 7 and especially PHP 7.2, are very good at optimizing code when it's complied into byte code. Unsed use statements are just stripped away by the compiler and will not even execute. Therefore it should not have any impact whatsoever. The compiler might use a few more CPU cycles while parsing the file but if you use OPCache there will be no effect on performance. The file will only be loaded when they are needed.

Gymnosperm answered 11/6, 2018 at 8:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.