Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I timed the 3 ways of using an index like this:</p> <pre><code>for ($ii = 0; $ii &lt; 1000000; $ii++) { // TEST 1 $array[$idx] = $ii; // TEST 2 $array["$idx"] = $ii; // TEST 3 $array["{$idx}"] = $ii; } </code></pre> <p>The first set of tests used <code>$idx=0</code>, the second set used <code>$idx="0"</code>, and the third set used <code>$idx="blah"</code>. Timing was done using <code>microtime()</code> diffs. I'm using WinXP, PHP 5.2, Apache 2.2, and Vim. :-)</p> <p>And here are the results:</p> <h3>Using <code>$idx = 0</code></h3> <pre><code>$array[$idx] // time: 0.45435905456543 seconds $array["$idx"] // time: 1.0537171363831 seconds $array["{$idx}"] // time: 1.0621709823608 seconds ratio "$idx" / $idx // 2.3191287282497 ratio "{$idx}" / $idx // 2.3377348193858 </code></pre> <h3>Using <code>$idx = "0"</code></h3> <pre><code>$array[$idx] // time: 0.5107250213623 seconds $array["$idx"] // time: 0.77445602416992 seconds $array["{$idx}"] // time: 0.77329802513123 seconds ratio "$idx" / $idx // = 1.5163855142717 ratio "{$idx}" / $idx // = 1.5141181512285 </code></pre> <h3>Using <code>$idx = "blah"</code></h3> <pre><code>$array[$idx] // time: 0.48077392578125 seconds $array["$idx"] // time: 0.73676419258118 seconds $array["{$idx}"] // time: 0.71499705314636 seconds ratio "$idx" / $idx // = 1.5324545551923 ratio "{$idx}" / $idx // = 1.4871793473086 </code></pre> <p>So <code>$array[$idx]</code> is the hands-down winner of the performance competition, at least on my machine. (The results were very repeatable, BTW, I ran it 3 or 4 times and got the same results.)</p>
 

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