fork(19) download
  1. /tmp $ cat Main.java
  2. import java.util.Calendar;
  3.  
  4. import org.joda.time.DateTime;
  5. import org.joda.time.DateTimeFieldType;
  6. import org.joda.time.DateTimeField;
  7. import org.joda.time.DateTimeZone;
  8. import org.joda.time.MutableDateTime;
  9. import org.joda.time.chrono.GregorianChronology;
  10.  
  11. interface MillisToYear {
  12. int year(long millis);
  13. }
  14.  
  15. class CalendarMillisToYear implements MillisToYear {
  16. private final Calendar cal = Calendar.getInstance();
  17.  
  18. @Override
  19. public int year(final long millis) {
  20. cal.setTimeInMillis(millis);
  21. return cal.get(Calendar.YEAR);
  22. }
  23. }
  24.  
  25. class JodaMillisToYear implements MillisToYear {
  26. private final DateTimeFieldType year = DateTimeFieldType.year();
  27.  
  28. @Override
  29. public int year(final long millis) {
  30. return new DateTime(millis).get(year);
  31. }
  32. }
  33.  
  34. class MutableJodaMillisToYear implements MillisToYear {
  35. private final DateTimeFieldType year = DateTimeFieldType.year();
  36. private final MutableDateTime date = new MutableDateTime();
  37.  
  38. @Override
  39. public int year(final long millis) {
  40. date.setMillis(millis);
  41. return date.get(year);
  42. }
  43. }
  44.  
  45. class DirectJodaMillisToYear implements MillisToYear {
  46. private final DateTimeZone timezone = DateTimeZone.getDefault();
  47. private final DateTimeField year =
  48. GregorianChronology.getInstanceUTC().year();
  49.  
  50. @Override
  51. public int year(long millis) {
  52. millis = timezone.convertUTCToLocal(millis);
  53. return year.get(millis);
  54. }
  55. }
  56.  
  57. public class Main {
  58. public static void main(final String... args) {
  59. MillisToYear mty;
  60. switch (Integer.parseInt(args[0])) {
  61. case 0:
  62. mty = new CalendarMillisToYear();
  63. break;
  64. case 1:
  65. mty = new JodaMillisToYear();
  66. break;
  67. case 2:
  68. mty = new MutableJodaMillisToYear();
  69. break;
  70. case 3:
  71. mty = new DirectJodaMillisToYear();
  72. break;
  73. default:
  74. throw new RuntimeException();
  75. }
  76. long now = System.currentTimeMillis() / 86400000L * 86400000L;
  77. for (long i = 0; i < 1000000; ++i) {
  78. mty.year(now + i);
  79. }
  80. long start = System.currentTimeMillis();
  81. long chksum = 0L;
  82. for (long i = 0L; i < 10000000000L; i += 1000L) {
  83. chksum += mty.year(now + i);
  84. }
  85. long total = (System.currentTimeMillis() - start);
  86. System.out.println("Total time: " + total);
  87. System.out.println("Checksum: " + chksum);
  88. }
  89. }
  90.  
  91. /tmp $ javac -cp joda-time-2.0.jar Main.java
  92. /tmp $ java -cp joda-time-2.0.jar:. Main 0
  93. Total time: 793
  94. Checksum: 20142065600
  95. /tmp $ java -cp joda-time-2.0.jar:. Main 1
  96. Total time: 394
  97. Checksum: 20142065600
  98. /tmp $ java -cp joda-time-2.0.jar:. Main 2
  99. Total time: 421
  100. Checksum: 20142065600
  101. /tmp $ java -cp joda-time-2.0.jar:. Main 3
  102. Total time: 156
  103. Checksum: 20142065600
  104. /tmp $ cat year.go
  105. package main
  106.  
  107. import (
  108. "fmt"
  109. "time"
  110. )
  111.  
  112. func main() {
  113. var unixTime int64 = time.Now().Unix() / 86400 * 86400
  114. start := time.Now()
  115. var chksum int64 = 0
  116.  
  117. for i := 0; i < 10000000; i++ {
  118. chksum += int64(time.Unix((unixTime + int64(i)), 0).Year())
  119. }
  120. total := time.Now().Sub(start)
  121. fmt.Printf("Total: %v\n", total)
  122. fmt.Printf("Checksum: %v\n", chksum);
  123. }
  124. /tmp $ go run year.go
  125. Total: 699.872908ms
  126. Checksum: 20142062000
  127.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty