When reading the php manual on array push i found that it suggest using $array[]=$push to enter new entries.
so my question is how would I go about using this with multidimensional arrays in the most efficient way ie speed.
Examlple 1:
$client[] = (0);
$client[] = (1);
$client[] = (2);
$client[] = (3);
$array[$i++]=$client;
unset($client);
Example 2:
$array[$i++]= array(0,1,2,3);
Example 3: Note: I dont currently know of a good way to set the array key on this one
$entry = array(0,1,2,3);
array_push($array,$entry);
The 4 values nested in the array will be updated very frequently. To do this I would assume using the following method would be my best choice regarding speed and efficiency.
$array[0][0]= $array[1][0]+1;
Spelling it out: I have individual clients with unique identifiers. I need to keep track of 4 integers for each client. I'm looking to the fastest/uses the lowest resources method.
All in all ill take any suggestion but im curious if example 1 is any better in terms of speed and resources then example 2.
Thanks, JT
ACTUAL CODE To TEST:
<?php
$array = array();
$i=0;
$t1 = microtime(true);
while ($i<10000){
$array[$i++]= array(0,1,2,3);
}
$time1 = microtime(true) - $t1;
$mem1 = memory_get_peak_usage(true);
//print_r($array);
$array = array();
//echo '<br><br>';
$i=0;
$t2 = microtime(true);
while ($i<10000){
$client[] = (0);
$client[] = (1);
$client[] = (2);
$client[] = (3);
$array[$i++]=$client;
unset($client);
}
$time2 = microtime(true) - $t2;
$mem2 = memory_get_peak_usage(true);
//print_r($array);
$array = array();
//echo ' <br><br>';
$i=0;
$t3 = microtime(true);
while ($i++<10000){
$entry = array(0,1,2,3);
array_push($array,$entry);
}
$time3 = microtime(true) - $t3;
$mem3 = memory_get_peak_usage(true);
//print_r($array);
//echo '<br><br>';
print 'example 1 - ' . $time1 . ' - ' . $mem1 . '<br/>';
print 'example 2 - ' . $time2 . ' - ' . $mem2 . '<br/>';
print 'example 3 - ' . $time3 . ' - ' . $mem3 . '<br/>';
?>
RESULTS:
example 2 - 0.212869294 S
example 1 - 0.251849988 S
example 3 - 0.748561144 S
So the array push is a NO GO! This was the average of about 15 runs with each loop counting to 100*1000 :)