Recently, a Python crawler crawled through all the students ' grades and photos to find some problems.
Python's memory management mechanism has not been well-developed, as the program runs, the use of memory gradually increased, it should be expected that the newly opened space is not released in time.
Study the algorithm first, prepare for the match.
The M-moving average is a set of data with an average of adjacent M-items moving in a simple loop that makes the length of the time-complexity array very large *m
Simple improvement in time complexity is just the length of the array n
1 Importjava.util.ArrayList;2 ImportJava.util.Scanner;3 4 Importcom.sun.accessibility.internal.resources.accessibility; 5 6 Public classRunningaverage {7 Public Static voidMain (string[] args) {8double[] Test = {2.0,4.0,6.0,2.0,2.0,2.0,4.0,5.0,6.0};9arraylist<double> rList = movingaverage (Test, 4);Ten for(Double double1:rlist) { One System.out.println (double1); A } - } - the - Public StaticArraylist<double> movingaverage (double[] A,intM) { - intN =a.length; -Double sum = 0.0; +Arraylist<double> sumlist =NewArraylist<double>(); - + //calculate the M-1 of the preceding items A for(inti = 0; i < M-1; i++) { atSum + =A[i]; - } - - for(inti = M-1; i < N; i++) { -Sum + = A[i];//plus the last one . -Sumlist.add (sum/M); inSum-= a[i-m+1];//minus the first item - } to returnsumlist; + } - the}
Simple linear m-moving average