Skip to content

Instantly share code, notes, and snippets.

@ahwayakchih
Created June 24, 2011 12:54
Show Gist options
  • Save ahwayakchih/1044709 to your computer and use it in GitHub Desktop.
Save ahwayakchih/1044709 to your computer and use it in GitHub Desktop.
Test PHP's function call vs direct access to public static variable
<?php
/*
* Test how function calls compare to direct variable access for simple things
* like switching some flag/option.
*
* Output should show something like:
*
* Direct access took: 0.14991188049316s
* Function call took: 0.55304503440857s
*
*/
class Foo {
public static $test;
public function begin() {
self::$test = true;
}
public function end() {
self::$test = false;
}
}
function dummy() {
$a = 42;
}
// Benchmark access to static variable
$start = microtime(true);
for ($i = 100000; $i > 0; $i--) {
Foo::$test = true;
dummy();
Foo::$test = false;
}
$end = microtime(true) - $start;
echo "Direct access took: {$end}s\n";
// Benchmark function calls
$start = microtime(true);
for ($i = 100000; $i > 0; $i--) {
Foo::begin();
dummy();
Foo::end();
}
$end = microtime(true) - $start;
echo "Function call took: {$end}s\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment