Skip to content

Instantly share code, notes, and snippets.

@Zae
Created October 3, 2010 19:26
Show Gist options
  • Save Zae/608839 to your computer and use it in GitHub Desktop.
Save Zae/608839 to your computer and use it in GitHub Desktop.
<?php
/**
* Function vs constant test
*
* Constant should be faster with the interpreter, but maybe the compiler inlines the function?
*
*/
class Test{
var $runs = 10000000;
const constant = 'IAMCONST';
function func(){ return 'IAMFUNC';}
public function testfunction(){
$start = microtime(true);
for($i=0; $i<$this->runs; $i++){
$a = $this->func();
}
return microtime(true) - $start;
}
public function testconst(){
$start = microtime(true);
for($i=0;$i<$this->runs; $i++){
$a = self::constant;
}
return microtime(true) - $start;
}
}
$test = new Test();
print_r(array("Constant" => $test->testconst()));
print_r(array("Function" => $test->testfunction()));
exit("EXIT");
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment