Skip to content

Instantly share code, notes, and snippets.

@soyuka
Created October 27, 2022 10:20
Show Gist options
  • Save soyuka/3fd2af290aecde1c8f025dd118edf8e7 to your computer and use it in GitHub Desktop.
Save soyuka/3fd2af290aecde1c8f025dd118edf8e7 to your computer and use it in GitHub Desktop.
it perf test
<?php
namespace Soyuka\Marsh\Tests;
$a = [];
for ($i=0; $i < 10000; $i++) {
$o = new \stdClass();
$o->i = $i;
$a[] = $o;
}
$output = new class () {
public function write() {}
};
$time_start = microtime(true);
$it = new \ArrayIterator($a);
$valid = $it->valid();
while ($valid) {
$output->write("\"{$it->key()}\":");
$output->write(json_encode($it->current()));
$it->next();
$valid = $it->valid();
if ($valid) {
$output->write(',');
}
}
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "Array iterator took $time seconds\n";
$time_start = microtime(true);
$start = "";
foreach ($a as $key => $value) {
$output->write("$start\"{$key}\":");
$output->write(json_encode($value));
$start = ",";
}
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "Foreach took $time seconds\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment