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
- straight processing
- function call and a global statement to access variables
-
// straight processing, variables
-
for($i=0;$i<$loop;$i++) {
-
$test = ((sin($val0 * $val1 + $val2))/$val6 * (sqrt( log($val3) * abs($val4 - 1/$val5)) / $val7)) * $val8;
-
}
-
-
// straight processing, arrays
-
for($i=0;$i<$loop;$i++) {
-
$test = ((sin($arrayval[0] * $arrayval[1] + $arrayval[2]))/$arrayval[6] * (sqrt( log($arrayval[3]) * abs($arrayval[4] - 1/$arrayval[5])) / $arrayval[7])) * $arrayval[8];
-
}
-
-
// straight processing, constants
-
for($i=0;$i<$loop;$i++) {
-
$test = ((sin(VAL0 * VAL1 + VAL2))/VAL6 * (sqrt( log(VAL3) * abs(VAL4 - 1/VAL5)) / VAL7)) * VAL8;
-
}
-
-
// function call, variables
-
function testvar($loop) {
-
global $val0,$val1,$val2,$val3,$val4,$val5,$val6,$val7,$val8;
-
for ($i=0;$i<$loop;$i++) {
-
$test = ((sin($val0 * $val1 + $val2))/$val6 * (sqrt( log($val3) * abs($val4 - 1/$val5)) / $val7)) * $val8;
-
}
-
}
-
-
// function call, array
-
function testarr($loop) {
-
global $arrayval;
-
for ($i=0;$i<$loop;$i++) {
-
$test = ((sin($arrayval[0] * $arrayval[1] + $arrayval[2]))/$arrayval[6] * (sqrt( log($arrayval[3]) * abs($arrayval[4] - 1/$arrayval[5])) / $arrayval[7])) * $arrayval[8];
-
}
-
}
-
-
// function call, constants
-
function testconst($loop) {
-
for ($i=0;$i<$loop;$i++) {
-
$sum = ((sin(VAL0 * VAL1 + VAL2))/VAL6 * (sqrt( log(VAL3) * abs(VAL4 - 1/VAL5)) / VAL7)) * VAL8;
-
}
-
}
(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 :
-
if ($variable = 1) {
-
// oops !
-
}
-
-
if (VARIABLE = 1) {
-
// easy to spot and won't work
-
}
But this is slight advantage, and you're going safer anyway with a reverse syntax where typos generate a warning :
-
if (1 == $variable) { // do stuff }
So, basically, I'm asking : why use constants ? Are you using them ?
Related posts
Shorter URL
Want to share or tweet this post? Please use this short URL: http://ozh.in/d9
replied, 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/).
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 :)
thought, 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.
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 [...]
replied, on 10/Oct/08 at 1:12 pm # :
unbelievable!!!
so? should we use variables?