Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does variable name length matter for performance in PHP?

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 ( );
like image 223
Stepchik Avatar asked Dec 10 '12 08:12

Stepchik


2 Answers

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.

like image 56
Seidr Avatar answered Sep 27 '22 19:09

Seidr


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.

like image 23
Charles Avatar answered Sep 27 '22 19:09

Charles