Check PHP Code Performance and stdClass using InfiniteIterator

Following code is written using stdClass and PHP iterator “InfiniteIterator”

$start_time = microtime(true);

$std = new stdClass();
$std->first = "first";
$std->second = "second";
$std->third = "third";

$iterator = new InfiniteIterator(new ArrayIterator($std));
foreach ( new LimitIterator($iterator, 0, 1400000) as $value ) {
 print($value . PHP_EOL);
}

$end_time = microtime(true);
print $end_time - $start_time . " Sec" . "<br>";

And this code wrap between microtime() function to checkout the time consumed for this operation from start to end. stdClass class object hold three dynamic variable which we assign at the run time, then we pass the stdClass object to Itrerator “ArrayIterator” and then to “InfiniteIterator”. using foreach loop we are printing 1400000 values by assigning the object of InfiniteIterator to LimitIterator. if we will not use “LimitIterator”, loop will not end.

stdClass is php’s generic empty class, It is useful for anonymous objects, dynamic properties, etc.

When you run this code, your browser will stop working for few second until LimitIterator loop will end. at then end of these all printed values you will time consumed by this code. you can change parameter for LimitIterator to check variable amount of printed values and check the time.