Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Based on your given code, we can reverse-engineer the structure of $arr2 to (assuming R, G and B are integer from 0 to 255):</p> <pre><code>$arr2 = array( 0 =&gt; array( 0 =&gt; array( "R" =&gt; 128, "G" =&gt; 64, "B" =&gt; 255 ), 1 =&gt; array( ... ) ) ); </code></pre> <p>Given that your <code>$SIZE</code> is set to <code>256</code>, you will have a total of <code>256*256=65536</code> arrays further containing arrays with key-values for <code>R</code>, <code>G</code> and <code>B</code>, resulting in total of <code>256*256*3=196608 integers</code> in 3 levels of hierarchy. No surprise your code is slow!</p> <p>I think the best strategy here is to try to reduce the total number of items in your array. </p> <p>Given that instead of encoding single cells as "R, G, B" triples, you could encode all values in a single integer. Such as instead of:</p> <pre><code>0 =&gt; array( "R" =&gt; $r, "G" =&gt; $g, "B" =&gt; $b ) </code></pre> <p>Given that <code>0&lt;=r,g,b&lt;=255</code>, you could encode <code>$arr2</code> as:</p> <pre><code>0 =&gt; ($r&lt;&lt;16 + $g&lt;&lt;8 + $b); </code></pre> <p>Now of course you need to unpack the color value inside your loop as well. This can be achieved by:</p> <pre><code>$col = $arr2[$y][$x]; $col_b = ($col&amp;255); $col_g = ($col&gt;&gt;8)&amp;255; $col_r = ($col&gt;&gt;16)&amp;255; $r .= $col_r.":"; $g .= $col_g.":"; $b .= $col_b.":"; </code></pre> <p>This modification alone would cut one level of hierarchy from your array completely.</p> <p>While running your original code with $SIZE=256, my average execution speed in my settings was 0.30 secs. <strong>With the given refactoring, I was able to reduce this to 0.10 secs cutting your calculation time to 1/3 of the original.</strong></p> <p>You will still have a lot of work to do if you wish to improve the performance, but I hope this gives you an idea on how you could proceed.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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