Guest User

Untitled

a guest
Aug 17th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4. output:
  5. results, fastest first:
  6. method_call 0.0907 152.0000
  7. function_call 0.0954 584.0000
  8.  
  9. method calls are insignificantly faster - but use (a lot) less memory, for some reason
  10. */
  11.  
  12. if (extension_loaded('xdebug')) {
  13. echo "WARNING: xdebug is enabled\n";
  14. }
  15.  
  16. class Container {
  17. public $foo = null;
  18. public function __construct()
  19. {
  20. $this->foo = new Foo();
  21. }
  22. }
  23.  
  24. class Foo {
  25. public function method_call(){ return "something"; }
  26. public static function function_call(){ return "something"; }
  27. }
  28.  
  29. $container = new Container();
  30.  
  31. function function_call()
  32. {
  33. global $container;
  34. Foo::function_call();
  35. }
  36.  
  37. function method_call()
  38. {
  39. global $container;
  40. $container->foo->method_call();
  41. }
  42.  
  43. $data = array();
  44. $functions = array(
  45. 'function_call',
  46. 'method_call',
  47. );
  48.  
  49. $res = array();
  50. $mem = array();
  51. foreach( $functions as $k )
  52. {
  53. $iterations = 100000;
  54. $rest = array();
  55. $t = memory_get_usage();
  56. //ob_start();
  57. $start = microtime(true);
  58. while (--$iterations) {
  59. $k();
  60. }
  61. $end = microtime(true);
  62. //ob_get_clean();
  63. $res[$k] = $end - $start;
  64. $mem[$k] = memory_get_usage() - $t;
  65. }
  66.  
  67. echo "results, fastest first:\n";
  68. asort($res);
  69. foreach ($res as $k => $d) {
  70. printf("%-20s %0.4f %0.4f\n", $k, $d, $mem[$k]);
  71. }
Add Comment
Please, Sign In to add comment