0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

localDateに関する初回起動時のオーバーヘッド

Posted at

Kotlin開発中にふと気になったことを備忘録的にまとめる

はじめに

localDateを使用して実装されているメソッドを仮想マシン上で動かしている時に、そこまで複雑な処理じゃないはずなのに妙に遅いと思う瞬間があったのでその原因を調べてみました

初回起動時に発生する問題

実際に組んでいた処理は以下のようなものです

import java.time.LocalDate
import java.time.format.DateTimeFormatter

private fun getCurrentDate(): Int {

    val currentDate = LocalDate.now()

    val formattedCurrentDate = currentDate.format(DateTimeFormatter.ofPattern("yyyy_MM_dd"))

    return formattedCurrentDate.toInt()

}

これを1回だけ呼び出す場合に、仮想マシン上で最初に実行した時だけ極端に遅くなっていました

原因は至極単純で、java.timeパッケージの処理を仮想マシンで最初に呼び出した時に発生する初回ロードと初期化によるものでした

特に

val formattedCurrentDate = currentDate.format(DateTimeFormatter.ofPattern("yyyy_MM_dd"))

この部分の処理と初期化関連でかなりロスが発生しているみたいでした

これらは最初の1度だけ発生するので妙に遅い時があったと言うことでした

対策

これといって対策する必要はないと思いますが、あえて挙げるなら、対象の処理に入る前のどこかの段階であらかじめjava.timeパッケージを呼び出しておく、つまりウォームアップしておくことで対策はできるかと思います

最後に

特に影響はないちょっとした疑問でしたが、解決できてスッキリしました

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?