user76322: My Arduino controller reads data coming from four different sensors. After the data is read, I individually compute four input average values. I understood that I am having the same lines of code repeated four times. Instead, I want to define a function with an analog pin as input and compute the average. My code is working fine. The question I have is, will this function improve the speed, reduce memory?
My code:
// Samples average
sample_cnt = 1000;

//// Input the pin number and compute average value
float Average_input_value(int analog_pin){
  float avg_val = 0;
  for(int i=0;i<sample_cnt;i++){
    avg_val += analogRead(analog_pin);        
  }
  // Divide by Sample count to get the average value
  avg_val = avg_val/sample_cnt;  
  return avg_val;
  }


user7508: Just adding some numbers to complement JRobert's answer.
On the Uno, the cost of calling a function and returning from it is
about 0.5 µs. In contrast, calling analogRead() within a loop
costs at least 112 µs per iteration. Given that you are computing
the sum as a float (where a suitably sized integer would suffice), it
may well be more that 112 µs in your case.
I do not know how many readings you want to average, but even if it is
only a handful, the calls to analogRead() will completely dwarf the
cost of using a function.

user313: Writing the code out four times, in line, is a little bit faster than calling a function because the function call and return take some time, and the loop instructions take some time, and neither of those contribute to the average calculation.
Writing a function that you use four times, however, is much more readable, is much easier to fix if you've made a mistake because you can fix it once instead of four times, and is much easier to change or replace for the same reason, if that ever becomes necessary. Besides, a good optimizing compiler can often decide when to rewrite your loop as inline several repetitions of the loop contents (called "unrolling the loop"), so hand optimizing is probably not worthwhile.
Where your function will win over 4 repetitions of the calculation code is in the code-space needed. The function is more compact, but slightly slower.

76322: <p>My Arduino controller reads data coming from four different sensors. After the data is read, I individually compute four input average values. I understood that I am having the same lines of code repeated four times. Instead, I want to define a function with an analog pin as input and compute the average. My code is working fine. The question I have is, will this function improve the speed, reduce memory?</p>
<p>My code:</p>
<pre><code>// Samples average
sample_cnt = 1000;

//// Input the pin number and compute average value
float Average_input_value(int analog_pin){
  float avg_val = 0;
  for(int i=0;i&lt;sample_cnt;i++){
    avg_val += analogRead(analog_pin);        
  }
  // Divide by Sample count to get the average value
  avg_val = avg_val/sample_cnt;  
  return avg_val;
  }
</code></pre>
 7508: <p>Just adding some numbers to complement JRobert's answer.</p>
<p>On the Uno, the cost of calling a function and returning from it is
about 0.5 µs. In contrast, calling <code>analogRead()</code> within a loop
costs <em>at least</em> 112 µs per iteration. Given that you are computing
the sum as a <code>float</code> (where a suitably sized integer would suffice), it
may well be more that 112 µs in your case.</p>
<p>I do not know how many readings you want to average, but even if it is
only a handful, the calls to <code>analogRead()</code> will completely dwarf the
cost of using a function.</p>
 313: <p>Writing the code out four times, in line, is a little bit faster than calling a function because the function call and return take some time, and the loop instructions take some time, and neither of those contribute to the average calculation.</p>
<p>Writing a function that you use four times, however, is much more readable, is much easier to fix if you've made a mistake because you can fix it once instead of four times, and is much easier to change or replace for the same reason, if that ever becomes necessary. Besides, a good optimizing compiler can often decide when to rewrite your loop as inline several repetitions of the loop contents (called &quot;unrolling the loop&quot;), so hand optimizing is probably not worthwhile.</p>
<p>Where your function will win over 4 repetitions of the calculation code is in the code-space needed. The function is more compact, but slightly slower.</p>
