/tmp $ cat Main.java
import java.util.Calendar;
import org.joda.time.DateTime;
import org.joda.time.DateTimeFieldType;
import org.joda.time.DateTimeField;
import org.joda.time.DateTimeZone;
import org.joda.time.MutableDateTime;
import org.joda.time.chrono.GregorianChronology;
interface MillisToYear {
int year(long millis);
}
class CalendarMillisToYear implements MillisToYear {
private final Calendar cal = Calendar.getInstance();
@Override
public int year(final long millis) {
cal.setTimeInMillis(millis);
return cal.get(Calendar.YEAR);
}
}
class JodaMillisToYear implements MillisToYear {
private final DateTimeFieldType year = DateTimeFieldType.year();
@Override
public int year(final long millis) {
return new DateTime(millis).get(year);
}
}
class MutableJodaMillisToYear implements MillisToYear {
private final DateTimeFieldType year = DateTimeFieldType.year();
private final MutableDateTime date = new MutableDateTime();
@Override
public int year(final long millis) {
date.setMillis(millis);
return date.get(year);
}
}
class DirectJodaMillisToYear implements MillisToYear {
private final DateTimeZone timezone = DateTimeZone.getDefault();
private final DateTimeField year =
GregorianChronology.getInstanceUTC().year();
@Override
public int year(long millis) {
millis = timezone.convertUTCToLocal(millis);
return year.get(millis);
}
}
public class Main {
public static void main(final String... args) {
MillisToYear mty;
switch (Integer.parseInt(args[0])) {
case 0:
mty = new CalendarMillisToYear();
break;
case 1:
mty = new JodaMillisToYear();
break;
case 2:
mty = new MutableJodaMillisToYear();
break;
case 3:
mty = new DirectJodaMillisToYear();
break;
default:
throw new RuntimeException();
}
long now = System.currentTimeMillis() / 86400000L * 86400000L;
for (long i = 0; i < 1000000; ++i) {
mty.year(now + i);
}
long start = System.currentTimeMillis();
long chksum = 0L;
for (long i = 0L; i < 10000000000L; i += 1000L) {
chksum += mty.year(now + i);
}
long total = (System.currentTimeMillis() - start);
System.out.println("Total time: " + total);
System.out.println("Checksum: " + chksum);
}
}
/tmp $ javac -cp joda-time-2.0.jar Main.java
/tmp $ java -cp joda-time-2.0.jar:. Main 0
Total time: 793
Checksum: 20142065600
/tmp $ java -cp joda-time-2.0.jar:. Main 1
Total time: 394
Checksum: 20142065600
/tmp $ java -cp joda-time-2.0.jar:. Main 2
Total time: 421
Checksum: 20142065600
/tmp $ java -cp joda-time-2.0.jar:. Main 3
Total time: 156
Checksum: 20142065600
/tmp $ cat year.go
package main
import (
"fmt"
"time"
)
func main() {
var unixTime int64 = time.Now().Unix() / 86400 * 86400
start := time.Now()
var chksum int64 = 0
for i := 0; i < 10000000; i++ {
chksum += int64(time.Unix((unixTime + int64(i)), 0).Year())
}
total := time.Now().Sub(start)
fmt.Printf("Total: %v\n", total)
fmt.Printf("Checksum: %v\n", chksum);
}
/tmp $ go run year.go
Total: 699.872908ms
Checksum: 20142062000