web-dev-qa-db-ja.com

Scalaでコードのランタイムを知るにはどうすればよいですか?

Scalaでコードのランタイムを計算する必要があります。コードです。

val data = sc.textFile("/home/david/Desktop/Datos Entrada/household/household90Parseado.txt")

val parsedData = data.map(s => Vectors.dense(s.split(' ').map(_.toDouble))).cache()

val numClusters = 5
val numIterations = 10 
val clusters = KMeans.train(parsedData, numClusters, numIterations)

このコードを処理するランタイムを知る必要があります。時間は秒でなければなりません。どうもありがとうございました。

12

ディスカッション ここ に基づいて、System.nanoTimeを使用して経過時間の差を測定する必要があります。

val t1 = System.nanoTime

/* your code */

val duration = (System.nanoTime - t1) / 1e9d
28
evan.oman

Scalameterを使用できます: https://scalameter.github.io/

かっこ内にコードブロックを置くだけです。

val executionTime = measure {
  //code goes here
}

Jvmをウォームアップするように構成して、測定の信頼性を高めることができます。

val executionTime = withWarmer(new Warmer.Default) measure {
  //code goes here
}
5
fr3ak

_Spark2_から始まるspark.time(<command>)(scala今までのみ)を使用して、 action/transformation ..の実行にかかる時間.

例:

_records in a dataframe_のカウントを検索しています

_scala> spark.time(
                 sc.parallelize(Seq("foo","bar")).toDF().count() //create df and count
                 )
Time taken: 54 ms //total time for the execution
res76: Long = 2  //count of records
_
5
Shu

最も基本的なアプローチは、開始時刻と終了時刻を単純に記録し、減算することです。

val startTimeMillis = System.currentTimeMillis()

/* your code goes here */

val endTimeMillis = System.currentTimeMillis()
val durationSeconds = (endTimeMillis - startTimeMillis) / 1000
3
Larsenal

これは、scalaコードの時間を計算するための最良の方法です。

def time[R](block: => (String, R)): R = {
    val t0 = System.currentTimeMillis()
    val result = block._2
    val t1 = System.currentTimeMillis()
    println(block._1 + " took Elapsed time of " + (t1 - t0) + " Millis")
    result
 }

 result = kuduMetrics.time {
    ("name for metric", your function call or your code)
 }
1