Last active
August 29, 2015 14:01
-
-
Save saibotsivad/b73856885dcd42f52df1 to your computer and use it in GitHub Desktop.
Timing test of Java's SimpleDateFormat
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
package com.edatasource.receipts.parser; | |
import org.junit.Test; | |
import java.text.SimpleDateFormat; | |
import java.util.ArrayList; | |
import java.util.Date; | |
import java.util.List; | |
/** | |
* SimpleDateFormat is not thread safe, so you'll need to either lock the thread when | |
* you run format.parse() or create a new SimpleDateFormat(string) each time. This | |
* test attempts to check the relative efficiency of each method. | |
* | |
* In multiple tests, with the below values, the results are as follows: | |
* Parsing: 4.7E7 +/- 0.2E7 | |
* Format: 6.3E7 +/- 0.2E7 | |
* | |
* In other words, thread locking appears to be fastest, but only marginally so (10,000 | |
* new SimpleDateFormat objects created and parsed) | |
*/ | |
public class DateUtilTest { | |
private static final Object lock = new Object(); | |
private static final List<String> stringList; | |
private static final List<SimpleDateFormat> formatList; | |
static { | |
stringList = new ArrayList<String>(); | |
for (int i = 0; i < 10000; i++) { | |
stringList.add("MMMMM, dd, yyyy"); | |
} | |
stringList.add("yyyy-dd-MM"); | |
formatList = new ArrayList<SimpleDateFormat>(); | |
for (String s : stringList) { | |
formatList.add(new SimpleDateFormat(s)); | |
} | |
} | |
@Test | |
public void whichWayIsFastest() { | |
String dateString = "2014-04-17"; | |
long parsingTime = 0; | |
long formatTime = 0; | |
for (int i = 0; i < 100; i++) { | |
parsingTime += parse(dateString); | |
formatTime += format(dateString); | |
} | |
double averageParsingTime = Long.valueOf(parsingTime).doubleValue() / 100; | |
double averageFormatTime = Long.valueOf(formatTime).doubleValue() / 100; | |
System.out.println("Parsing: " + averageParsingTime); | |
System.out.println("Format: " + averageFormatTime); | |
} | |
private static long format(String dateString) { | |
long startTime = System.nanoTime(); | |
Date date = null; | |
for (String s : stringList) { | |
try { | |
date = new SimpleDateFormat(s).parse(dateString); | |
} catch (Exception ignore) {} | |
if (date != null) { | |
break; | |
} | |
} | |
long stopTime = System.nanoTime(); | |
return stopTime - startTime; | |
} | |
private static long parse(String dateString) { | |
long startTime = System.nanoTime(); | |
Date date = null; | |
for (SimpleDateFormat dateFormat : formatList) { | |
synchronized (lock) { | |
try { | |
date = dateFormat.parse(dateString); | |
} catch (Exception e) {} | |
} | |
if (date != null) { | |
break; | |
} | |
} | |
long stopTime = System.nanoTime(); | |
return stopTime - startTime; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment