Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Since it doesn't seem like anyone has done this before I thought it'd be good idea to have it for reference somewhere. I've gone though and either via benchmark or code-skimming to characterize the <code>array_*</code> functions. I've tried to put the more interesting Big-O near the top. This list is not complete.</p> <p>Note: All the Big-O where calculated assuming a hash lookup is O(1) even though it's really O(n). The coefficient of the n is so low, the ram overhead of storing a large enough array would hurt you before the characteristics of lookup Big-O would start taking effect. For example the difference between a call to <code>array_key_exists</code> at N=1 and N=1,000,000 is ~50% time increase.</p> <p><strong>Interesting Points</strong>:</p> <ol> <li><code>isset</code>/<code>array_key_exists</code> is much faster than <code>in_array</code> and <code>array_search</code></li> <li><code>+</code>(union) is a bit faster than <code>array_merge</code> (and looks nicer). But it does work differently so keep that in mind.</li> <li><code>shuffle</code> is on the same Big-O tier as <code>array_rand</code></li> <li><code>array_pop</code>/<code>array_push</code> is faster than <code>array_shift</code>/<code>array_unshift</code> due to re-index penalty</li> </ol> <p><strong>Lookups</strong>:</p> <p><code>array_key_exists</code> O(n) but really close to O(1) - this is because of linear polling in collisions, but because the chance of collisions is very small, the coefficient is also very small. I find you treat hash lookups as O(1) to give a more realistic big-O. For example the different between N=1000 and N=100000 is only about 50% slow down.</p> <p><code>isset( $array[$index] )</code> O(n) but really close to O(1) - it uses the same lookup as array_key_exists. Since it's language construct, will cache the lookup if the key is hardcoded, resulting in speed up in cases where the same key is used repeatedly. </p> <p><code>in_array</code> O(n) - this is because it does a linear search though the array until it finds the value.</p> <p><code>array_search</code> O(n) - it uses the same core function as in_array but returns value.</p> <p><strong>Queue functions</strong>:</p> <p><code>array_push</code> O(∑ var_i, for all i)</p> <p><code>array_pop</code> O(1) </p> <p><code>array_shift</code> O(n) - it has to reindex all the keys</p> <p><code>array_unshift</code> O(n + ∑ var_i, for all i) - it has to reindex all the keys</p> <p><strong>Array Intersection, Union, Subtraction</strong>:</p> <p><code>array_intersect_key</code> if intersection 100% do O(Max(param_i_size)*∑param_i_count, for all i), if intersection 0% intersect O(∑param_i_size, for all i)</p> <p><code>array_intersect</code> if intersection 100% do O(n^2*∑param_i_count, for all i), if intersection 0% intersect O(n^2)</p> <p><code>array_intersect_assoc</code> if intersection 100% do O(Max(param_i_size)*∑param_i_count, for all i), if intersection 0% intersect O(∑param_i_size, for all i)</p> <p><code>array_diff</code> O(π param_i_size, for all i) - That's product of all the param_sizes</p> <p><code>array_diff_key</code> O(∑ param_i_size, for i != 1) - this is because we don't need to iterate over the first array.</p> <p><code>array_merge</code> O( ∑ array_i, i != 1 ) - doesn't need to iterate over the first array</p> <p><code>+</code> (union) O(n), where n is size of the 2nd array (ie array_first + array_second) - less overhead than array_merge since it doesn't have to renumber</p> <p><code>array_replace</code> O( ∑ array_i, for all i )</p> <p><strong>Random</strong>:</p> <p><code>shuffle</code> O(n)</p> <p><code>array_rand</code> O(n) - Requires a linear poll.</p> <p><strong>Obvious Big-O</strong>:</p> <p><code>array_fill</code> O(n)</p> <p><code>array_fill_keys</code> O(n)</p> <p><code>range</code> O(n)</p> <p><code>array_splice</code> O(offset + length)</p> <p><code>array_slice</code> O(offset + length) or O(n) if length = NULL</p> <p><code>array_keys</code> O(n)</p> <p><code>array_values</code> O(n)</p> <p><code>array_reverse</code> O(n)</p> <p><code>array_pad</code> O(pad_size)</p> <p><code>array_flip</code> O(n)</p> <p><code>array_sum</code> O(n)</p> <p><code>array_product</code> O(n)</p> <p><code>array_reduce</code> O(n)</p> <p><code>array_filter</code> O(n)</p> <p><code>array_map</code> O(n)</p> <p><code>array_chunk</code> O(n) </p> <p><code>array_combine</code> O(n)</p> <p>I'd like to thank <a href="https://www.creativemachineslab.com/eureqa.html" rel="noreferrer">Eureqa</a> for making it easy to find the Big-O of the functions. It's an amazing <em>free</em> program that can find the best fitting function for arbitrary data.</p> <p>EDIT:</p> <p>For those who doubt that PHP array lookups are <code>O(N)</code>, I've written a benchmark to test that (they are still effectively <code>O(1)</code> for most realistic values).</p> <p><img src="https://i.stack.imgur.com/PgRQd.png" alt="php array lookup graph"></p> <pre><code>$tests = 1000000; $max = 5000001; for( $i = 1; $i &lt;= $max; $i += 10000 ) { //create lookup array $array = array_fill( 0, $i, NULL ); //build test indexes $test_indexes = array(); for( $j = 0; $j &lt; $tests; $j++ ) { $test_indexes[] = rand( 0, $i-1 ); } //benchmark array lookups $start = microtime( TRUE ); foreach( $test_indexes as $test_index ) { $value = $array[ $test_index ]; unset( $value ); } $stop = microtime( TRUE ); unset( $array, $test_indexes, $test_index ); printf( "%d,%1.15f\n", $i, $stop - $start ); //time per 1mil lookups unset( $stop, $start ); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload