Created
May 7, 2012 21:56
-
-
Save amorton/2630736 to your computer and use it in GitHub Desktop.
nanoTime() Test.
This file contains hidden or 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
public class NanoTest | |
{ | |
/** | |
* usage: NanoTest - run for 100ms and count unique results from System.nanoTest() | |
* NanoText x - run for x ms and count unique results from System.nanoTest() | |
* @param args | |
*/ | |
public static void main(String[] args) | |
{ | |
final long duration = (args.length > 0 ? Long.parseLong(args[0]) : 100); | |
final long startMilli = System.currentTimeMillis(); | |
long lastNano = System.nanoTime(); | |
long lastCount = 0; | |
long totalDuplicates = 0; | |
long totalUniques = 1; | |
while ((System.currentTimeMillis() - startMilli) < duration) | |
{ | |
final long thisNano = System.nanoTime(); | |
if (thisNano == lastNano) | |
{ | |
lastCount++; | |
} | |
else | |
{ | |
System.out.println(String.format("nanoTime %s occurred %s times", lastNano, lastCount)); | |
lastNano = thisNano; | |
totalUniques++; | |
totalDuplicates += lastCount; | |
lastCount = 0; | |
} | |
} | |
System.out.println(String.format("Ran for %s milliseconds, got %s duplicates and %s uniques.", duration, totalDuplicates, totalUniques)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment