Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- output:
- results, fastest first:
- method_call 0.0907 152.0000
- function_call 0.0954 584.0000
- method calls are insignificantly faster - but use (a lot) less memory, for some reason
- */
- if (extension_loaded('xdebug')) {
- echo "WARNING: xdebug is enabled\n";
- }
- class Container {
- public $foo = null;
- public function __construct()
- {
- $this->foo = new Foo();
- }
- }
- class Foo {
- public function method_call(){ return "something"; }
- public static function function_call(){ return "something"; }
- }
- $container = new Container();
- function function_call()
- {
- global $container;
- Foo::function_call();
- }
- function method_call()
- {
- global $container;
- $container->foo->method_call();
- }
- $data = array();
- $functions = array(
- 'function_call',
- 'method_call',
- );
- $res = array();
- $mem = array();
- foreach( $functions as $k )
- {
- $iterations = 100000;
- $rest = array();
- $t = memory_get_usage();
- //ob_start();
- $start = microtime(true);
- while (--$iterations) {
- $k();
- }
- $end = microtime(true);
- //ob_get_clean();
- $res[$k] = $end - $start;
- $mem[$k] = memory_get_usage() - $t;
- }
- echo "results, fastest first:\n";
- asort($res);
- foreach ($res as $k => $d) {
- printf("%-20s %0.4f %0.4f\n", $k, $d, $mem[$k]);
- }
Add Comment
Please, Sign In to add comment