- microtime: documentation ( source)
- asort: documentation ( source)
- sort: documentation ( source)
- define: documentation ( source)
- rand: documentation ( source)
- printf: documentation ( source)
<?php
define('INT_COUNT',100000);
// Fill an array with random ints
$ints2 = array();
for ($i = 0; $i < INT_COUNT; $i++){
$ints2[] = rand(0, 255);
}
// asort the ints
asort($ints2);
$start = microtime(true);
// Do nothing but access each int in the array
for ($i = 0; $i < INT_COUNT; $i++){
$ints2[$i];
}
$finish = microtime(true);
printf("Time taken with asort(): %.7f seconds\n", $finish - $start);
unset($ints2);
// Now do it again, but this time use sort
// Fill an array with random ints
$ints = array();
for ($i = 0; $i < INT_COUNT; $i++){
$ints[] = rand(0, 255);
}
// Sort the ints
sort($ints);
$start = microtime(true);
// Do nothing but access each int in the array
for ($i = 0; $i < INT_COUNT; $i++){
$ints[$i];
}
$finish = microtime(true);
printf("Time taken with sort(): %.7f seconds\n", $finish - $start);