Created
June 24, 2011 12:54
-
-
Save ahwayakchih/1044709 to your computer and use it in GitHub Desktop.
Test PHP's function call vs direct access to public static variable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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