Simple linear m-moving average

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.