- strpos: documentation ( source)
- microtime: documentation ( source)
- rand: documentation ( source)
<?php
function realValue($value)
{
if (
is_string($value) &&
strpos($value, '"SuperClosure\Serialize"') !== false
){
return 'closure!';
}
return $value;
}
function rand_string($len) {
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$size = strlen($chars);
$str = null;
for ($i = 0; $i < $len; $i++) {
$str .= $chars[ rand( 0, $size - 1 ) ];
}
return $str;
}
$records = [];
for ($i = 1; $i <= 2000; $i++) {
$records[$i] = rand_string(1000);
}
// Ignore above, just generate a random string with 20,000 records.
$read_value = null;
$realValue_bench_start = microtime(true);
foreach ($records as $record) {
$read_value = realValue($record);
}
$realValue_bench_end = microtime(true);
$directAccess_bench_start = microtime(true);
foreach ($records as $record) {
$read_value = $record;
}
$directAccess_bench_end = microtime(true);
$diffRealValue = $realValue_bench_end - $realValue_bench_start;
$diffDirectAccess = $directAccess_bench_end - $directAccess_bench_start;
echo("readValue time: $diffRealValue");
echo("\ndirectAccess time: $diffDirectAccess");
echo("\ndifference:" . ($diffRealValue - $diffDirectAccess));