Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You could reduce the number of indexed accesses to <code>result</code>, as well as the <code>Length</code> properties:</p> <pre><code>int inputLength = filter.Length; int filterLength = filter.Length; var result = new double[inputLength + filterLength - 1]; for (int i = resultLength; i &gt;= 0; i--) { double sum = 0; // max(i - input.Length + 1,0) int n1 = i &lt; inputLength ? 0 : i - inputLength + 1; // min(i, filter.Length - 1) int n2 = i &lt; filterLength ? i : filterLength - 1; for (int j = n1; j &lt;= n2; j++) { sum += input[i - j] * filter[j]; } result[i] = sum; } </code></pre> <hr> <p>If you further split the outer loop, you can get rid of some repeating conditionals. (This assumes 0 &lt; <code>filterLength</code> &leq; <code>inputLength</code> &leq; <code>resultLength</code>)</p> <pre><code>int inputLength = filter.Length; int filterLength = filter.Length; int resultLength = inputLength + filterLength - 1; var result = new double[resultLength]; for (int i = 0; i &lt; filterLength; i++) { double sum = 0; for (int j = i; j &gt;= 0; j--) { sum += input[i - j] * filter[j]; } result[i] = sum; } for (int i = filterLength; i &lt; inputLength; i++) { double sum = 0; for (int j = filterLength - 1; j &gt;= 0; j--) { sum += input[i - j] * filter[j]; } result[i] = sum; } for (int i = inputLength; i &lt; resultLength; i++) { double sum = 0; for (int j = i - inputLength + 1; j &lt; filterLength; j++) { sum += input[i - j] * filter[j]; } result[i] = sum; } </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