Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@disktnk
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save disktnk/c40690637a24ea5906f3 to your computer and use it in GitHub Desktop.
Save disktnk/c40690637a24ea5906f3 to your computer and use it in GitHub Desktop.
java7までの処理計測と、java8からの処理計測
import java.time.Duration;
import java.time.Instant;
public class DurationSample {
public static void main(String[] args) {
{ // ~java1.7
long start = System.nanoTime();
someAlgorithm();
long end = System.nanoTime();
double elapse = (end - start) / 1000000.0;
System.out.println(elapse + "[ms]");
}
{ // java1.8~
Instant start = Instant.now();
someAlgorithm();
Instant end = Instant.now();
Duration elapse = Duration.between(start, end);
System.out.println(elapse.toNanos() / 1000000.0 + "[ms]");
}
}
private static void someAlgorithm() {
// some algorithm
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment