- microtime: documentation ( source)
- array_flip: documentation ( source)
- in_array: documentation ( source)
<?php
$array = array('test1', 'test2', 'test3', 'test4');
$var = 'test';
$iterations = 1000000;
$start = microtime(true);
for($i = 0; $i < $iterations; ++$i) {
if ($var != 'test1' && $var != 'test2' && $var != 'test3' && $var != 'test4') {}
}
$end = microtime(true);
print "multiple comparisons: \n". ($end - $start)."\n";
$start2 = microtime(true);
for($i = 0; $i < $iterations; ++$i) {
if (!in_array($var, $array) ) {}
}
$end2 = microtime(true);
print "in_array: \n".($end2 - $start2)."\n";
$start = microtime(true);
$array_flip = array_flip($array);
for($i = 0; $i < $iterations; ++$i) {
if (!isset($array_flip[$var])) {}
}
$end = microtime(true);
print "array_flip() outside loop measured + isset(): \n".($end - $start)."\n";
$array_flip = array_flip($array);
$start = microtime(true);
for($i = 0; $i < $iterations; ++$i) {
if (!isset($array_flip[$var])) {}
}
$end = microtime(true);
print "array_flip() outside loop not measured + isset(): \n".($end - $start)."\n";
$start = microtime(true);
for($i = 0; $i < $iterations; ++$i) {
foreach ($array as $entry) {
if ($entry == $var) {}
}
}
$end = microtime(true);
print "foreach and compoarison: \n".($end - $start)."\n";
This script was stopped while abusing our resources