Last active
December 8, 2015 05:26
-
-
Save ponkotuy/1e2b7cf3a39ab5dcf17c to your computer and use it in GitHub Desktop.
Compare Changing Date to yyyymmddhh(Int)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
遅すぎわろたwwwwwwww