If you remember my prime spiral generator, you would remember it cost over 1 hour to generate a 2000*2000 prime spiral.
But after I did the simple diffusion limited aggregation script, prime spirals' speed seems impossibly long.
Thx to Edd for pointing out array_shift() is über slow, after I slightly changed the code from
while($n<=$size){
if($prime[0]<=$sn){
$p = array_shift($prime);
to
$i = 0;
while($n<=$size){
if($prime[$i]<=$sn){
$p = $prime[$i++];
The 2000*2000 prime spiral is generated in 12 seconds!
This script will make everything more clear:
$array= range(1,10000);
$timeparts = explode(' ',microtime());
$starttime = $timeparts[1].substr($timeparts[0],1);
while($i<10000){
$array[$i];
++$i;
}
$timeparts = explode(' ',microtime());
$endtime = $timeparts[1].substr($timeparts[0],1);
echo 'Native array loop:',bcsub($endtime,$starttime,6),'<br />';
$timeparts = explode(' ',microtime());
$starttime = $timeparts[1].substr($timeparts[0],1);
$i= 0;
while($i<10000){
array_shift($array);
++$i;
}
$timeparts = explode(' ',microtime());
$endtime = $timeparts[1].substr($timeparts[0],1);
echo 'array_shift():',bcsub($endtime,$starttime,6);
The result:
Native array loop:0.003198
array_shift():2.128530
The native loop is over 650 times faster, don't you think it's too much? What could possibly turn array_shift so inefficient? array_splice($array,0,1) is 7 times slower than array_shift().
So, don't use those functions unless you absolutely have to.
Recent comments
7 hours 36 min ago
15 hours 6 min ago
1 day 8 hours ago
1 day 18 hours ago
3 days 3 hours ago
4 days 7 hours ago
4 days 11 hours ago
4 days 11 hours ago
4 days 11 hours ago
5 days 3 hours ago