0

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 :)

6
  • Example 1 does not use $array[1], so the result is different. Can you post complete code for both Examples to make it clear what exactly you want to compare? Commented Sep 2, 2013 at 15:04
  • yep give me just a few min. @DmitriZaitsev Commented Sep 2, 2013 at 15:12
  • @DmitriZaitsev Code is wrote Commented Sep 2, 2013 at 15:40
  • In my case the fastest was array_push. Commented Sep 2, 2013 at 15:51
  • @KeluThatsall add an answer with what you tried Commented Sep 2, 2013 at 15:54

1 Answer 1

1

here is a quick dirty test for this

<?php
$t1 = microtime(true);

$array = array();

for ($i = 0; $i < 100000; $i++) {
  $client[] = (0);
  $client[] = (1);
  $client[] = (2);
  $client[] = (3);
  $array[]=$client;
  unset($client);
}

$time1 = microtime(true) - $t1;
$mem1 = memory_get_peak_usage(true);

$array = array();
$t2 = microtime(true);

for ($i = 0; $i < 100000; $i++) {
  $array[] = array(0=>array(0,1,2,3));
}

$time2 = microtime(true) - $t2;
$mem2 = memory_get_peak_usage(true);

$array = array();
$t3 = microtime(true);

for ($i = 0; $i < 100000; $i++) {
  $array[] = [0=>array(0,1,2,3)];
}

$time3 = microtime(true) - $t3;
$mem3 = memory_get_peak_usage(true);

print 'example 1 - ' . $time1 . ' - ' . $mem1 . '<br/>';
print 'example 2 - ' . $time2 . ' - ' . $mem2 . '<br/>';
print 'example 3 - ' . $time3 . ' - ' . $mem3 . '<br/>';

for me i found that the first example was the least time efficient but the most memory efficient and that example 3 slightly out performed 2 in time but was equal in memory

Sign up to request clarification or add additional context in comments.

6 Comments

When i looked over my examples i noticed an error. The problem with the test you can was that one had an array growing in size while the others overwrote themselves. I updated the code at the bottom of my answer
you bring up a strong point, i am ashamed of myself. ive changed my cod too
Did you rerun the test?
i did, i found that the first example was the least time efficient but the most memory efficient and that example 3 slightly out performed 2 in time but was equal in memory
i already have, there is verry little difference just the addition of [] to $array to prevent the override and a couple of $array = array()'s to reset $array before each test
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.