In: , , , ,
On: 2006 / 06 / 01 Viewed: 114350 times

I thought, and had been told, that using constants instead of variables in PHP was mostly a matter of speed : at run time, the engine replaces every occurrence of a constant with its hard-coded value, making code presumably faster than using variables that must be evaluated each time they are encountered. Or something like this.

Wondering how big or meaningless the incidence would be, and being the anal retentive useless benchmarks & statistics fan I am, I ran a few tests. With a surprise for me at the end. Bottom line : variables are faster ?!

Tests

To compare speed when processing some $variables, $arrays and CONSTANTS, I threw them twice into the silly formula ((sin(A * B + C))/D * (sqrt( log(E) * abs(F - 1/G)) / H)) * I

  1. straight processing
  2. function call and a global statement to access variables
PHP:
  1. // straight processing, variables
  2. for($i=0;$i<$loop;$i++) {
  3.     $test = ((sin($val0 * $val1 + $val2))/$val6 * (sqrt( log($val3) * abs($val4 - 1/$val5)) / $val7)) * $val8;
  4. }
  5.  
  6. // straight processing, arrays
  7. for($i=0;$i<$loop;$i++) {
  8.     $test = ((sin($arrayval[0] * $arrayval[1] + $arrayval[2]))/$arrayval[6] * (sqrt( log($arrayval[3]) * abs($arrayval[4] - 1/$arrayval[5])) / $arrayval[7])) * $arrayval[8];
  9. }
  10.  
  11. // straight processing, constants
  12. for($i=0;$i<$loop;$i++) {
  13.     $test = ((sin(VAL0 * VAL1 + VAL2))/VAL6 * (sqrt( log(VAL3) * abs(VAL4 - 1/VAL5)) / VAL7)) * VAL8;
  14. }
  15.  
  16. // function call, variables
  17. function testvar($loop) {
  18.     global $val0,$val1,$val2,$val3,$val4,$val5,$val6,$val7,$val8;
  19.     for ($i=0;$i<$loop;$i++) {
  20.         $test = ((sin($val0 * $val1 + $val2))/$val6 * (sqrt( log($val3) * abs($val4 - 1/$val5)) / $val7)) * $val8;
  21.     }
  22. }
  23.  
  24. // function call, array
  25. function testarr($loop) {
  26.     global $arrayval;
  27.     for ($i=0;$i<$loop;$i++) {
  28.         $test = ((sin($arrayval[0] * $arrayval[1] + $arrayval[2]))/$arrayval[6] * (sqrt( log($arrayval[3]) * abs($arrayval[4] - 1/$arrayval[5])) / $arrayval[7])) * $arrayval[8];
  29.     }
  30. }
  31.  
  32. // function call, constants
  33. function testconst($loop) {
  34.     for ($i=0;$i<$loop;$i++) {
  35.         $sum = ((sin(VAL0 * VAL1 + VAL2))/VAL6 * (sqrt( log(VAL3) * abs(VAL4 - 1/VAL5)) / VAL7)) * VAL8;
  36.     }
  37. }

(Download the full benchmark script)

Results

  PHP 4.4.2 PHP 5.1.2
straight, variables 0.0382 0.0269
straight, array 0.0510 0.0363
straight, constants 0.0552 0.03848
function call, variables 0.0382 0.0276
function call, array 0.0462 0.0331
function call, constants 0.0470 0.0388

Hey, where is the supposedly speed advantage of constants ? Not only using variables is faster, but even arrays which I thought would perform worse consistently outperform constants.

Conclusions

I was not able to compare memory usage across these tests, but regarding speed I'm certainly disappointed to see how constants are doing. Of course, speed loss is probably negligible in almost if not all situations, but still.

Why use constants then ? Their only advantages over variables I can think of is that constant have a global scope so you can forget a global statement, and if you mistype a test (= instead of == or ===), it's safer to use a constant that cannot be redefined than a variable. Consider the following :

PHP:
  1. if ($variable = 1) {
  2. // oops !
  3. }
  4.  
  5. if (VARIABLE = 1) {
  6. // easy to spot and won't work
  7. }

But this is slight advantage, and you're going safer anyway with a reverse syntax where typos generate a warning :

PHP:
  1. if (1 == $variable) { // do stuff }

So, basically, I'm asking : why use constants ? Are you using them ?

Related posts

Metastuff

This entry "PHP : Variables vs Constants" was posted on 01/06/2006 at 9:29 pm and is tagged with , , , ,
Watch this discussion : Comments RSS 2.0. You can trackback this post from your own site

4 Blablas

  1. 1
    BB2k France »
    wrote, on 02/Jun/06 at 10:08 am # :

    lol at the antispam =)

    The difference between variables and CONSTANTS is really an advantage when you use compiled language since the CONSTANT is evaluated one time (at the compilation). this is the matter of the some optimisations algorithms that replace know variables in fonctions to speed up them.
    ie . if in C=A+3, A as a knonw value of 3 in this environnement (yes actually this is a constant, but only in a given envirronment) then the code will be transformed in C=6.

    In an intepreted language like php, the CONSTANt is replaced each time you run the code and then the optimisation is not so good. It should be better with the use of a pre-compiler like phpaccelerator (http://www.php-accelerator.co.uk/).

  2. 2
    Roland Hesz Hungary »
    thought, on 02/Jun/06 at 10:11 am # :

    I use constants, mainly to avoid magic numbers, and for values that I use in a lot of places, but can change [urls, directories, filenames, etc.], so I won't forget to change one instance out of 10 :)

  3. 3
    lf France »
    commented, on 06/Jun/06 at 11:57 pm # :

    j'aime bien ton blog, y a pleins de trucs ou je comprends rien, et en plus c'est en anglais, alors ça me rassure.

  4. 4
    PHP Sidor » Blog Archive » S... Sweden »
    pingback on 12/Aug/07 at 9:22 pm # :

    [...] är egentligen snabbast, är det någon störra skillnad? Jag såg en bloggpost om detta, men kom fram till att jag ska fortsätta med arrayer eftersom det inte verkar vara någon större [...]

Leave a Reply

Comment Guidelines or Die

  • HTML: You can use these tags: <a href=""> <em> <i> <b> <strong> <blockquote>
  • Posting code: Post raw code (no <> &lt; etc) within appropriate tags : [php][/php], [css][/css], [html][/html], [js][/js], [sql][/sql], [xml][/xml], or generic [code][code]
  • Gravatars: Curious about the little images next to each commenter's name ? Go to Gravatar.
  • Spam: Various spam plugins on patrol. I'll put pins in a Voodoo doll if you spam me.
  • I will mark as Spam test comments, all comments with SEO names (ie "My Cool Online Shop" instead of "Joe") or containing forum-like signatures.

Read more ?

Close
E-mail It