# Performance of array_shift and array_pop in PHP

We have confirmed that array\_shift is much slower than array\_pop in <span class="caps">PHP</span>.

Code:

```
<br></br><?<p>// Create an array with 100000 elements</p><p>$array = array();<br></br>for ($i = 0; $i < 100000; $i++) {<br></br>	$array[] = rand();<br></br>}</p><p>// Remove the last 1000 elements using array_pop</p><p>$start = microtime(true);<br></br>for ($i = 0; $i < 1000; $i++) {<br></br>	array_pop($array);<br></br>}<br></br>$stop = microtime(true);<br></br>printf(“array_pop takes %.5f seconds\n”, $stop – $start);</p><p>// Add back 1000 elements</p><p>for ($i = 0; $i < 1000; $i++) {<br></br>	$array[] = rand();<br></br>}</p><p>// Remove the first 1000 elements using array_shift</p><p>$start = microtime(true);<br></br>for ($i = 0; $i < 1000; $i++) {<br></br>	array_shift($array);<br></br>}<br></br>$stop = microtime(true);<br></br>printf(“array_shift takes %.5f seconds\n”, $stop – $start);</p><p>// Add back 1000 elements</p><p>for ($i = 0; $i < 1000; $i++) {<br></br>	$array[] = rand();<br></br>}</p><p>// Remove the first 1000 elements by reversing the array and popping 1000 elements</p><p>$start = microtime(true);<br></br>$array_rev = array_reverse($array);<br></br>for ($i = 0; $i < 1000; $i++) {<br></br>	array_pop($array_rev);<br></br>}<br></br>$stop = microtime(true);<br></br>printf(“array_reverse + array_pop takes %.5f seconds\n”, $stop – $start);</p><p>?><br></br></p>
```

Result:

```
<br></br>array_pop takes 0.00089 seconds<br></br>array_shift takes 15.15544 seconds<br></br>array_reverse + array_pop takes 0.03934 seconds<br></br>
```