Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Array access in PHP can certainly be slow. PHP uses hash tables to implement arrays, i.e. in order to access an element in an array it has to calculate a hash and traverse a linked list. Using a compiled language with real arrays will definitely improve performance, because there a direct memory access is made. For the interested: Code for <a href="http://lxr.php.net/xref/PHP_5_3/Zend/zend_hash.c#zend_hash_find">hash access with string</a> and <a href="http://lxr.php.net/xref/PHP_5_3/Zend/zend_hash.c#zend_hash_index_find">with integer</a>.</p> <p>Concerning your code, there are several points I would optimize:</p> <ul> <li><code>return</code> directly, don't <code>break</code> twice.</li> <li>put <code>$file-&gt;get_width()</code> and <code>$file-&gt;get_height</code> into simple variables. I assume that the height or width doesn't change throughout the process. Remember: Functions in PHP are slow.</li> <li><del>Use a one-dimensional array, instead of nested arrays. You save one hash lookup per iteration that way.</del> Actually a one-dimensional array is only marginally faster or even slightly slower. <a href="https://gist.github.com/813289">Comparison of several ways of saving the data concerning performance and memory usage</a>.</li> </ul> <p>.</p> <pre><code>function fits($bin, $x, $y, $w, $h) { $w += $x; $h += $y; for ($i = $x; $i &lt; $w; ++$i) { for ($j = $y; $j &lt; $h; ++$j) { if ($bin[$i][$j] !== 0) { return false; } } } return true; } </code></pre> <p>Though I'm not sure, why you add <code>$x</code> to the <code>$width</code> / <code>$y</code> to the <code>$height</code>. Don't you want to iterate from the current coordinates to the image boundaries?</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