1

I'Ve made a few test on global vs function parameter and the difference is futile.

But as i was testing stuff, i found that $GLOBALS is about 10% slower. than using function parameter or global keyword. Anyone care to explain why?

I wish to understand further the mechanism behind PHP. This so i can make better compromise on futur dev. Not that i ever use $GLOBALS except in some exception case.

$md5 = md5(1000000);
function byGlobal() {
  global $md5;
  $c = 0;
  while( md5($c) != $md5 ){
    $c++;
  }
}
function superGlobal() {
  $c = 0;
  while( md5($c) != $GLOBALS[ 'md5' ] ){
    $c++;
  }
}
function asParam($md5) {
  $c = 0;
  while( md5($c) != $md5 ){
    $c++;
  }
}
$time3 = microtime(true);
asParam($md5);
echo (microtime(true) - $time3);

echo "<br/>";
$time1 = microtime(true);
byGlobal();
echo (microtime(true) - $time1);

echo "<br/>";
$time2 = microtime(true);
superGlobal();
echo (microtime(true) - $time2);
echo "<br/>";

I'm not arguing about function vs global or good practice. I really just wonder why so much difference. Ran the test 20 times and results are pretty much all consistent.

  • i move the calls up and down in the code to avoid caching influence.
  • I'm doing a million md5 iteration on each call so the server works a load.
  • Time results are cosistent with other hashing function (tested sha1, crc32) and they are all in the same +10% slower)
  • Ran on a VM with PHP 5.4.16 on CentOs7.

1st run

as param : 0.8601s

as global : 0.8262s

as $GLOBALS : 0.9463s (more than 10% slower)

2nd run

as param : 0.8100s

as global : 0.8058s

as $GLOBALS : 0.9624s (more than 10% slower again)

Related studies : (mainely debate about global best practice, nothing about $GLOBALS vs global performance.

The advantage / disadvantage between global variables and function parameters in PHP?

php global variable overhead in a framework

Does using global create any overhead?

Community
  • 1
  • 1
Louis Loudog Trottier
  • 1,367
  • 13
  • 26

1 Answers1

2

EDIT: New version, I made a mistake in the first one.

I've rewritten your tests, so it does test the things you want:

<?php

$value = 6235;

function byGlobal() {
  global $value;
  return $value++;
}

function superGlobal() {
  return $GLOBALS['value']++;
}

function asParam($parameter) {
  return $parameter++;
}

$time = microtime(true);
for ($i = 0;$i < 10000000;$i++) $value = asParam($value);
echo 'asParam: '.(microtime(true)-$time).'<br/>';

$time = microtime(true);
for ($i = 0;$i < 10000000;$i++) $value = byGlobal();
echo 'byGlobal: '.(microtime(true)-$time).'<br/>';

$time = microtime(true);
for ($i = 0;$i < 10000000;$i++) $value = superGlobal();
echo 'superGlobal: '.(microtime(true)-$time).'<br/>';

Example results for PHP 7.0.17 on CentOs7:

asParam: 0.43703699111938
byGlobal: 0.55213189125061
superGlobal: 0.70462608337402

and

asParam: 0.4569981098175
byGlobal: 0.55681920051575
superGlobal: 0.76146912574768

So, the superGlobal is the slowest, but not by that much. I guess the reason is that it is an array.

What I take away from this is that PHP is fast! I would not worry about the small differences in these tiny time slices. Having readable code is far more important. I my experiences the slowest thing in a website are the database queries.

KIKO Software
  • 15,283
  • 3
  • 18
  • 33