I tried to Google for an answer but I think I am using wrong keywords. What I am trying to do is – I have a collection of numbers (ints), and as I add new numbers I am computing the new average. I noticed that as my array grows, once I get to a certain count of elements (lets say 200), the calculation time becomes noticeable. I was wondering if there are any built-in SDK functions I can utilize to speed up my codes performance? I will be running the code on Android.
int[] numbers = new int[3];
private int average(int number){
//some buildin operation to push in array an int?
for(int i=0; i < numbers.length -1 ; i++){
numbers[i]=numbers[i+1];
}
numbers[numbers.length -1] = number;
;
//numbers[0] = numbers[1];
//numbers[1] = numbers[2];
//numbers[2] = number;
int sum = 0;
//any operation to get average?
for(int i=0; i < numbers.length ; i++)
sum = sum + numbers[i];
//calculate average value
double average = sum / numbers.length;
return (int)average;
}
Now I just made myself a circular buffer (as suggested Oli Charlesworth).
And this is my implementation:
UPD