SHARE
TWEET

PHP perfomance test

anton-pribora Mar 7th, 2016 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top