Note Submitter: schultz dot peter at hotmail dot com
----
I was unable to reproduce skrebbel's benchmarks in PHP 5.3.0 on Fedora x86. My benchmarks showed, that foreach iterations over ArrayObjects take about 160 to 175 % of the time of a foreach iteration over an array, nowhere near 20000%. Maybe the performance was improved since skrebbel's post, maybe one of our benchmarks was just badly written.
array fill : 0.236281
array read : 0.254270
array iterate : 0.023582
object fill : 0.221212
object read : 0.253145
object iterate : 0.040560
Code to reproduce:
<?php
require_once "Benchmark/Timer.php";
$bench = array();
$profile = array();
$timer = new Benchmark_Timer(true);
$recordBenchmark = function($stepName) use(&$profile, &$timer) {
$timer->setMarker($stepName);
$benchmark = end($timer->getProfiling());
$profile[$stepName] = $benchmark['diff'];
};
$benchSize = 100000;
foreach(array('array', 'object') as $type) {
$timer->setMarker('type benchmark start');
for ($i = 0; $i < $benchSize; $i++) {
$bench["-$i"] = "$i";
}
$recordBenchmark("$type fill");
for ($i = 0; $i < $benchSize; $i++) {
$dummy = $bench["$i"];
}
$recordBenchmark("$type read");
foreach($bench as $key => $value) {
}
$recordBenchmark("$type iterate");
$bench = new ArrayObject();
}
foreach($profile as $step => $time) {
printf("%-20s: %f\n", $step, $time);
}
?>
lmpx.com only provides a reader for public news (NNTP) servers. It is not
affiliated with the servers or forums shown here and is not responsible for
the content of articles, which is written by their respective authors.