Skip to content

Instantly share code, notes, and snippets.

@amorton
Created May 7, 2012 21:56
Show Gist options
  • Save amorton/2630736 to your computer and use it in GitHub Desktop.
Save amorton/2630736 to your computer and use it in GitHub Desktop.
nanoTime() Test.
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