目次
実行環境
- windows10 64bit
- メモリ 32G
- CPU corei7-7700CPU 3.60GHz
- 言語 Scala
測定コード
import scala.annotation.tailrec object sample { val setValue: Int = 1000000 def main(args: Array[String]): Unit = { // 1回目:11221msec // 2回目:12720msec // 3回目:12115msec printExecTime { for (i <- 0 to setValue) println(i) } // 1回目:12219msec // 2回目:11035msec // 3回目:11376msec printExecTime { var i = 0 while (i <= setValue) { println(i) i += 1 } } // 1回目:13576msec // 2回目:11626msec // 3回目:11563msec printExecTime { loop(0) @tailrec def loop(n: Int): Unit = { if (n <= setValue) { println(n) loop(n + 1) } } } // 1回目:10298msec // 2回目:12121msec // 3回目:15158msec printExecTime { (0 to setValue).foreach(println) } } /** * 処理速度測定を行う * * @param process */ def printExecTime(process: => Unit): Unit = { val start = System.currentTimeMillis process println("処理時間: " + (System.currentTimeMillis - start) + " ミリ秒") } }
結果
この結果だけで判断するのは難しいが
foreachが一番早かった。
全体的にはforまたはwhileが安定して早いかもしれない。
今回のような実験は普段やる機会が無いのでやってよかった。
以上