Skip to content

Instantly share code, notes, and snippets.

@ledlogic
Last active January 3, 2016 22:59
Show Gist options
  • Save ledlogic/8532028 to your computer and use it in GitHub Desktop.
Save ledlogic/8532028 to your computer and use it in GitHub Desktop.
System.currentTimeMillis versus (new Date()).getTime()
package ledlogic.com.utility;
import java.util.Date;
import org.apache.log4j.Logger;
import org.junit.Test;
public class MsTest {
private static final Logger LOG = Logger.getLogger(MsTest.class);
@Test
public void testSystemMillisecondsVersusNewDate() {
long[] it = { 0L, 0L};
long outerRuns = 1000L;
long innerRuns = 1000000L;
for (long oc=0; oc<outerRuns; oc++) {
long s0 = System.currentTimeMillis();
long temp = 0;
for (long ic=0; ic<innerRuns; ic++) {
temp = getSystemCurrentTime();
}
long s1 = System.currentTimeMillis();
for (long ic=0; ic<innerRuns; ic++) {
temp = getNewDateCurrentTime();
}
long s2 = System.currentTimeMillis();
long sys = (s1-s0);
long dat = (s2-s1);
LOG.debug("sys:" + sys + ", dat:" + dat);
it[0] += sys;
it[1] += dat;
}
LOG.debug("TOTAL sys:" + it[0] + ", dat:" + it[1]);
}
private Long getSystemCurrentTime() {
return System.currentTimeMillis();
}
private Long getNewDateCurrentTime() {
return (new Date()).getTime();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment