anton-pribora

PHP perfomance test

Mar 7th, 2016
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.15 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Исходный код для статьи http://anton-pribora.ru/articles/php/php-rerfomance/
  4.  */
  5.  
  6. $start   = microtime(true);
  7. $cycles  = 0;
  8. $elapsed = 0;
  9. $result  = '';
  10.  
  11. do {
  12.     $loops = 1000;
  13.     $begin = microtime(true);
  14.     do {
  15.         // Тестируемая операция
  16.         $result .= new A(array('foo' => 'bar'));
  17.     } while (--$loops);
  18.     $cycles  += 1000;
  19.     $elapsed += microtime(true) - $begin;
  20. } while (microtime(true) - $start < 1);
  21.  
  22. printf("%s: %.0f cycle/sec, %s bytes, %s bytes/cycle\n", PHP_VERSION, $cycles / $elapsed, strlen($result), strlen($result) / $cycles);
  23.  
  24. class A
  25. {
  26.     private $foo;
  27.     private $bar;
  28.     private $baz;
  29.     private $bee;
  30.  
  31.     public function __construct($array)
  32.     {
  33.         foreach($this as $prop => $value) {
  34.             if ( isset($array[$prop]) ) {
  35.                 $this->{$prop} = $array[$prop];
  36.             }
  37.         }
  38.     }
  39.  
  40.     public function __toString()
  41.     {
  42.         $result = array();
  43.  
  44.         foreach($this as $prop => $value) {
  45.             $result[] = $prop .': '. $this->{$prop};
  46.         }
  47.  
  48.         return join("\n", $result);
  49.     }
  50. }
Add Comment
Please, Sign In to add comment