Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is caching answers taking LONGER in MATLAB?
    primarykey
    data
    text
    <p>I have a long running function in MATLAB that I tried to speed up by adding caching and wound up slowing down my performance significantly. My code is basically searching for continuous "horizontal" lines in an edge detected image and the original code looks something like this:</p> <pre><code>function lineLength = getLineLength(img, startRow, startCol) [nRows, nCols] = size(img); lineLength = 0; if startRow &lt; 1 || startRow &gt; nRows return; end for curCol = startCol:nCols if img(curCol) lineLength = lineLength + 1; continue; elseif lineLength &gt; 0 lengths = zeros(2,1); lengths(1) = getLineLength(img, startRow - 1, curCol); lengths(2) = getLineLength(img, startRow + 1, curCol); increment = max(lengths); lineLength = lineLength + increment; end break; %// At this point the end of the current line has been reached end end function </code></pre> <p>Since performance in this function is not what I would like, I thought I would add caching of the length from any point something like the following:</p> <pre><code>function lineLength = getLineLength(img, startRow, startCol) persistent pointCache; if startRow == 0 &amp;&amp; startCol == 0 pointCache = zeros(size(img, 1), size(img, 2), 2); end [nRows, nCols] = size(img); lineLength = 0; if startRow &lt; 1 || startRow &gt; nRows return; end for curCol = startCol:nCols if pointCache(startRow, curCol, 2) lineLength = lineLength + pointCache(startRow, curCol, 1); break; end if img(curCol) lineLength = lineLength + 1; continue; elseif lineLength &gt; 0 lengths = zeros(2,1); lengths(1) = getLineLength(img, startRow - 1, curCol); lengths(2) = getLineLength(img, startRow + 1, curCol); increment = max(lengths); lineLength = lineLength + increment; end break; %// At this point the end of the current line has been reached end pointCache(startRow, startCol, 1) = lineLength; pointCache(startRow, startCol, 2) = 1; end function </code></pre> <p>What surprised me is that implementing this caching actually made my performance worse, rather than better. My best guesses are that either the <code>global</code> variable is getting me in trouble, or its the extra memory use, but I don't have enough MATLAB experience to know.</p> <p><strong>Edited...</strong></p> <p>As Gautam correctly pointed out that there was a bug in the original code that was ignoring the results of the recursion. This is what the actual code does. I'm sure it's obvious, but MATLAB is not my native language so if there's a more MATLABy way to do this, I'd love the suggestions.</p>
    singulars
    1. This table or related slice is empty.
    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. 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