Advertisement
Guest User

Untitled

a guest
May 15th, 2011
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.92 KB | None | 0 0
  1. <?php
  2.  
  3. function benchmark ($args, $reps = 100000) {
  4.     foreach ($args as $name => $fn) {
  5.         $start = microtime(true);
  6.        
  7.         for ($i = $reps; $i--; ) {
  8.             $fn();
  9.         }
  10.        
  11.         echo $name, ': ', microtime(true) - $start, "s\n";
  12.     }
  13. }
  14.  
  15. $myArray = array (
  16.     (object) array ('id' => 88, 'label' => 'Bus'),
  17.     (object) array ('id' => 91, 'label' => 'Submarine'),
  18.     (object) array ('id' => 92, 'label' => 'Boat')
  19. );
  20.  
  21. benchmark(array(
  22.     'lonesomeday' => function() use ($myArray) {
  23.         $value = '';
  24.         foreach ($myArray as $el) {
  25.             if ($el->id === 91) { // or other number
  26.                 $value = $el->label;
  27.                 break;
  28.             }
  29.         }
  30.     },
  31.     'AJ' => function() use ($myArray) {
  32.         $objects = array_filter($myArray, function($item){ return $item->id == 91; });
  33.         $ret = array_shift($objects);
  34.     }
  35. ), 1000000);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement