Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function benchmark ($args, $reps = 100000) {
- foreach ($args as $name => $fn) {
- $start = microtime(true);
- for ($i = $reps; $i--; ) {
- $fn();
- }
- echo $name, ': ', microtime(true) - $start, "s\n";
- }
- }
- $myArray = array (
- (object) array ('id' => 88, 'label' => 'Bus'),
- (object) array ('id' => 91, 'label' => 'Submarine'),
- (object) array ('id' => 92, 'label' => 'Boat')
- );
- benchmark(array(
- 'lonesomeday' => function() use ($myArray) {
- $value = '';
- foreach ($myArray as $el) {
- if ($el->id === 91) { // or other number
- $value = $el->label;
- break;
- }
- }
- },
- 'AJ' => function() use ($myArray) {
- $objects = array_filter($myArray, function($item){ return $item->id == 91; });
- $ret = array_shift($objects);
- }
- ), 1000000);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement