Skip to content

Instantly share code, notes, and snippets.

@shanethehat
Last active December 16, 2015 23:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shanethehat/5516588 to your computer and use it in GitHub Desktop.
Save shanethehat/5516588 to your computer and use it in GitHub Desktop.
Speed test assigning to local variable vs reference variable vs array index
<?php
$arr = array('a' => 'c');
$arr2 = array('a' => 'c');
$ref = &$arr2['a'];
$startArr = microtime(true);
foreach (range(1,10000) as $i) {
$arr['a'] = 'b';
}
$endArr = microtime(true);
$startRef = microtime(true);
foreach (range(1,10000) as $i) {
$ref = 'b';
}
$endRef = microtime(true);
$startVar = microtime(true);
foreach (range(1,10000) as $i) {
$a = 'b';
}
$endVar = microtime(true);
echo 'Array: ' . ($endArr - $startArr) . PHP_EOL;
echo 'Ref: ' . ($endRef - $startRef) . PHP_EOL;
echo 'Var: ' . ($endVar - $startVar) . PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment