정의 된 크기의 따옴표 목록 이동 평균을 계산하는 간단한 스칼라 프로그램을 작성했습니다. 초당 약 5-6 인용의 속도로 시세가 올 것입니다.스칼라 - 선택할 수있는 변경 가능 ListBuffer 또는 변경 불가능 목록?
1) 불변의 스칼라 목록에 따옴표를 넣어두면 좋을까요? 견적이 올 때마다 새로운 목록이 만들어 질 것입니다. 너무 많은 불필요한 기억을 차지할 것인가?
또는
2)이 좋은 내가 가장 오래된 견적을 제거하고 새로운 따옴표에게 견적이 올 때마다 밀어 것이다 것을 특징으로 ListBuffer 같은 변경 가능한 목록에서 따옴표를 유지하는 것입니다.
현재 코드
package com.example.csv
import scala.io.Source
import scala.collection.mutable.ListBuffer
object CsvFileParser {
val WINDOW_SIZE = 25;
var quotes = ListBuffer(0.0);
def main(args: Array[String]) = {
val src = Source.fromFile("GBP_USD_Week1.csv");
//drop header and split the comma separated tokens
val iter = src.getLines().drop(1).map(_.split(","));
// Sliding window reads ahead // remove it
val index = 0;
while(iter.hasNext) {
processRecord(iter.next)
}
src.close()
}
def processRecord(record: Array[String]) = {
if(quotes.length < WINDOW_SIZE){
quotes += record(4).toDouble;
}else {
val movingAverage = quotes.sum/quotes.length
quotes.map(_ + " ").foreach(print)
println("\nMoving Average " + movingAverage)
quotes = quotes.tail;
quotes += record(4).toDouble;
}
}
/*def simpleMovingAverage(values: ListBuffer[Double], period: Int): ListBuffer[Double] = {
ListBuffer.fill(period - 1)(0.0) ++ (values.sliding(period).map(_.sum).map(_/period))
}*/
}
이것은 불변 가변 컬렉션의 각종 동작에 대한 좋은 참고이다 http://www.scala-lang.org/docu/files/collections-api/collections_40.html. 최상의 성능 특성을 가진 작업을 사용하십시오. – Brian