Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Benchmark Java Thread-Safe Time Formatter Implementations
@Grapes([
@Grab(group='org.gperfutils', module='gbench', version='[0.4,)'),
@Grab(group='org.apache.commons', module='commons-lang3', version='[3.7,)'),
@Grab(group='joda-time', module='joda-time', version='[2.9.9,)')
])
/**
* Java 8 DateTimeFormatter
*/
import java.util.Date;
import java.time.temporal.TemporalAccessor;
import java.time.Instant;
import java.time.ZonedDateTime;
// final TemporalAccessor tmpnow = Instant.now();
// final Date now = new Date(tmpnow.toEpochMilli());
final TemporalAccessor tmpnow = ZonedDateTime.now();
final Date now = new Date(Instant.from(tmpnow).toEpochMilli());
import java.time.format.DateTimeFormatter;
// dtfi = DateTimeFormatter.ISO_INSTANT;
dtfi = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ");
public String dtf(TemporalAccessor date) { dtfi.format(date); }
/**
* Java SimpleDateFormat
*/
import java.text.SimpleDateFormat;
// using clone seems to be twice faster than constructing
// public String sdf(Date date) { new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").format(date); }
sdti = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
public String sdf(Date date) { sdti.clone().format(date); }
/**
* Joda-Time
*/
import org.joda.time.DateTime;
// import org.joda.time.format.ISODateTimeFormat;
import org.joda.time.format.DateTimeFormat;
// DateTime jodanow = DateTime.now()
DateTime jodanow = new DateTime(now);
// jodadtfi = ISODateTimeFormat.dateTimeNoMillis();
jodadtfi = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ssZ");
public String jodadtf(DateTime date) { date.toString(jodadtfi) }
/**
* Apache Commons Lang 3
*/
import org.apache.commons.lang3.time.FastDateFormat;
apachedtfi = FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ssZ");
public String apachedtf(Date date) { apachedtfi.format(date) }
println "SimpleTimeFormat: ${sdf(now)}"
println "DateTimeFormatter: ${dtf(tmpnow)}"
println "JodaDateTimeFormatter: ${jodadtf(jodanow)}"
println "FastTimeFormatter: ${apachedtf(now)}"
r = benchmark {
"SimpleDateFormat" {
sdf(now)
}
"DateTimeFormatter" {
dtf(tmpnow)
}
"Joda-Time" {
jodadtf(jodanow)
}
"FastDateFormat" {
apachedtf(now)
}
"SimpleDateFormat_now" {
sdf(new Date())
}
"DateTimeFormatter_now" {
dtf(ZonedDateTime.now())
}
"Joda-Time_now" {
jodadtf(DateTime.now())
}
"FastDateFormat_now" {
apachedtf(new Date())
}
}
r.prettyPrint()
@akostadinov

This comment has been minimized.

Copy link
Owner Author

akostadinov commented Dec 14, 2017

sample output:

SimpleTimeFormat: 2017-12-14T18:19:56+0200
DateTimeFormatter: 2017-12-14T18:19:56+0200
JodaDateTimeFormatter: 2017-12-14T18:19:56+0200
FastTimeFormatter: 2017-12-14T18:19:56+0200

Environment
===========
* Groovy: 2.4.12
* JVM: OpenJDK 64-Bit Server VM (25.151-b12, Oracle Corporation)
    * JRE: 1.8.0_151
    * Total Memory: 263.5 MB
    * Maximum Memory: 3550.5 MB
* OS: Linux (4.13.12-300.fc27.x86_64, amd64)

Options
=======
* Warm Up: Auto (- 60 sec)
* CPU Time Measurement: On

                       user  system   cpu  real

SimpleDateFormat       1129       0  1129  1135
DateTimeFormatter       468       5   473   474
Joda-Time               441       3   444   446
FastDateFormat          551       0   551   553
SimpleDateFormat_now   1151      10  1161  1166
DateTimeFormatter_now   677       0   677   680
Joda-Time_now           500       0   500   500
FastDateFormat_now      560       0   560   562
@tzachi81

This comment has been minimized.

Copy link

tzachi81 commented Oct 22, 2018

Hi Aleksandar, thanks for this post. Very helpful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.