Advertisement
sectus

Untitled

Feb 13th, 2012
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.18 KB | None | 0 0
  1. <?php
  2.  
  3. class aClass
  4.     {
  5.  
  6.     protected $protected_property = 'protected_value';
  7.     private $private_property = 'private_value';
  8.     public $public_property = 'public_value';
  9.  
  10.     public function getAllProperties()
  11.         {
  12.         return get_object_vars($this);
  13.         }
  14.  
  15.     }
  16.  
  17. $an_object = new aClass;
  18. //------------------------------------------------------------------------------
  19. $start = microtime(true);
  20. $an_array = array();
  21. for ($i = 0; $i <= 10000; $i++)
  22.     {
  23.     $reflection = new \ReflectionClass($an_object);
  24.     $properties = $reflection->getProperties();
  25.     foreach ($properties as $property)
  26.         {
  27.         $property->setAccessible(true);
  28.         $an_array[$property->getName()] = $property->getValue($an_object);
  29.         if (!$property->isPublic())
  30.             $property->setAccessible(false);
  31.         }
  32.     $reflection = null;
  33.     $properties = null;
  34.     }
  35. echo microtime(true) - $start, PHP_EOL; // 0.171475172043
  36. var_dump($an_array);
  37. //------------------------------------------------------------------------------
  38.  
  39. $start = microtime(true);
  40. $an_array = array();
  41. for ($i = 0; $i <= 10000; $i++)
  42.     {
  43.     reset($an_object);
  44.     while (list($key, $val) = each($an_object))
  45.         {
  46.         $key = ($key{0} === "\0") ? substr($key, strpos($key, "\0", 1) + 1) : $key;
  47.         $an_array[$key] = $val;
  48.         }
  49.     }
  50. echo microtime(true) - $start, PHP_EOL; // 0.0651688575745
  51. var_dump($an_array);
  52. //------------------------------------------------------------------------------
  53. $start = microtime(true);
  54. $an_array = array();
  55. for ($i = 0; $i <= 10000; $i++)
  56.     {
  57.     $an_array = $an_object->getAllProperties();
  58.     }
  59. echo microtime(true) - $start, PHP_EOL; // 0.0214040279388
  60. var_dump($an_array);
  61. //------------------------------------------------------------------------------
  62. $an_array = array(
  63.     'protected_property' => 'other_protected_value',
  64.     'private_property' => 'other_private_value',
  65.     'public_property' => 'other_public_value',
  66. );
  67. //------------------------------------------------------------------------------
  68. $start = microtime(true);
  69. for ($i = 0; $i <= 10000; $i++)
  70.     {
  71.     $reflection = new \ReflectionClass($an_object);
  72.     $properties = $reflection->getProperties();
  73.     foreach ($properties as $property)
  74.         {
  75.         $property->setAccessible(true);
  76.         $property->setValue($an_object, $an_array[$property->getName()]);
  77.         if (!$property->isPublic())
  78.             $property->setAccessible(false);
  79.         }
  80.     $reflection = null;
  81.     $properties = null;
  82.     }
  83. echo microtime(true) - $start, PHP_EOL; // 0.166314125061
  84. var_dump($an_object);
  85. //------------------------------------------------------------------------------
  86. $start = microtime(true);
  87. for ($i = 0; $i <= 10000; $i++)
  88.     {
  89.     array_walk($an_object, function(&$val, $key, $array)
  90.                 {
  91.                 $key = ($key{0} === "\0") ? substr($key, strpos($key, "\0", 1) + 1) : $key;
  92.                 $val = $array[$key];
  93.                 }, $an_array);
  94.     }
  95. echo microtime(true) - $start, PHP_EOL; // 0.0659220218658
  96. var_dump($an_object);
  97. //------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement