Does variable name length matter for performance in PHP?
Asked Answered
C

2

8

I have been seen this Does variable name length matter for performance C#? topic and have same question about php.

My co-worker (Front-end) have been encoded everything like $o, $r, $x, $m, $c and motivated it best performance. I really very doubt about it and code became difficult to read.

  1. $o - object or obj
  2. $m - $model
  3. $r - $query_result or $result
  4. $x - $xml_node

Every thing look like

            if ( isset ( self::$o[ self::$x -> name ] ) ) :

            $c = 'ClassPrefix_' . self::$o[ self::$x -> name ];

            $o = new $c;

            self::$x -> read ( );
Commemorate answered 10/12, 2012 at 8:57 Comment(2)
You can check this easily yourselfDarden
How many seconds do you plan to save by using shorter names?Hundredweight
M
9

Variable names exist more as a programmer aide - when a program or script is interpreted, these are converted into memory locations. Shorter variables will only negatively impact performance of the programmer(s) when modifying / debugging code in the future.

Obfuscation is a widely used technique that involves replacing variable names with single/multi-character variable names, but that is used in an attempt to make reverse engineering / lifting core technology from projects where the source code is readily available / easily extracted more difficult.

Milo answered 10/12, 2012 at 9:1 Comment(1)
Thanks, for your answer. It's really look like reverse engineering, especially we have another project(created early) that do 90% same thinks and 10% we can make them work easillyCommemorate
P
6

While it may save a byte here and a byte there, no, it does not matter one iota, and you will not see real memory or performance savings by using short variable names.

If you are really worried about memory use and performance, profile your code. xdebug and xhprof are great tools to get it done. You can even install xhprof on your production machines. Once you've actually measured your performance and memory use, then you can target the real problems in your code.

Also, make sure you're running 5.4, if you can. It introduces some huge improvements to both performance and memory use.

Pinto answered 10/12, 2012 at 8:59 Comment(1)
Thanks for your advice we will try install xhprof to production machine and test our code using php 5.4Commemorate

© 2022 - 2024 — McMap. All rights reserved.