Skip to content

Instantly share code, notes, and snippets.

@ponkotuy
Last active December 8, 2015 05:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ponkotuy/1e2b7cf3a39ab5dcf17c to your computer and use it in GitHub Desktop.
Save ponkotuy/1e2b7cf3a39ab5dcf17c to your computer and use it in GitHub Desktop.
Compare Changing Date to yyyymmddhh(Int)
import java.text.SimpleDateFormat
import java.util.{Calendar, Date}
import com.github.nscala_time.time.Imports._
object Main extends App {
def timer[A](name: String)(f: => A): A = {
val start = System.currentTimeMillis()
val result = f
println(s"${name}: ${System.currentTimeMillis() - start}ms")
result
}
val Count = 10000000
val jUtil = new Date()
val calendar = Calendar.getInstance()
val joda = DateTime.now()
val answer = joda.getYear * 1000000 + joda.getMonthOfYear * 10000 + joda.getDayOfMonth * 100 + joda.getHourOfDay
timer("Calculate by java.util.Date") {
(1 to Count).foreach { _ =>
assert(answer == (jUtil.getYear + 1900) * 1000000 + (jUtil.getMonth + 1) * 10000 + jUtil.getDate * 100 + jUtil.getHours)
}
}
timer("Using formatter by java.util.Date") {
val formatter = new SimpleDateFormat("yyyyMMddHH")
(1 to Count).foreach { _ =>
val value = formatter.format(jUtil).toInt
assert(answer == value, value)
}
}
timer("Calculate by java.util.Calendar") {
(1 to Count).foreach { _ =>
val value = calendar.get(Calendar.YEAR) * 1000000 + (calendar.get(Calendar.MONTH) + 1) * 10000 + calendar.get(Calendar.DAY_OF_MONTH) * 100 + calendar.get(Calendar.HOUR_OF_DAY)
assert(answer == value, value)
}
}
timer("Using formatter by java.util.Calendar") {
val formatter = new SimpleDateFormat("yyyyMMddHH")
(1 to Count).foreach { _ =>
assert(answer == formatter.format(calendar.getTime).toInt)
}
}
timer("Calculate by joda") {
(1 to Count).foreach { _ =>
assert(answer == joda.getYear * 1000000 + joda.getMonthOfYear * 10000 + joda.getDayOfMonth * 100 + joda.getHourOfDay)
}
}
timer("Using toString by joda ") {
(1 to Count).foreach { _ =>
assert(answer == joda.toString("yyyyMMddHH").toInt)
}
}
}
Calculate by java.util.Date: 106ms
Using formatter by java.util.Date: 2529ms
Calculate by java.util.Calendar: 81ms
Using formatter by java.util.Calendar: 2543ms
Calculate by joda: 607ms
Using toString by joda : 1756ms
@aoiroaoino
Copy link

Calculate by joda: 607ms

遅すぎわろたwwwwwwww

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment