Skip to content

Instantly share code, notes, and snippets.

@kuenishi
Last active March 23, 2022 06:10
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 kuenishi/b0c5316992e64d51796158d78b85c7e1 to your computer and use it in GitHub Desktop.
Save kuenishi/b0c5316992e64d51796158d78b85c7e1 to your computer and use it in GitHub Desktop.
$ java HelloWorld
5.316 seconds for 1000000000 iteration: 188111.36192626035 Kcalls/sec
2.866 seconds for 10000000 iteration: 3489.183531053733 Kcalls/sec
class HelloWorld {
public static void main(String[] args) {
bench1();
bench2();
}
static void bench1() {
long repeat = 1000000000L;
long start = java.lang.System.currentTimeMillis();
for (long i = 0; i < repeat; i++) {
Long.toHexString(i);
}
long end = java.lang.System.currentTimeMillis();
double duration = (end - start) / 1000.0; //seconds
System.out.println(duration + " seconds for " + repeat + " iteration: " + repeat / 1000.0 / duration + " Kcalls/sec");
}
static void bench2() {
long repeat = 10000000L;
long start = java.lang.System.currentTimeMillis();
for (long i = 0; i < repeat; i++) {
String.format("%016X", i);
}
long end = java.lang.System.currentTimeMillis();
double duration = (end - start) / 1000.0; //seconds
System.out.println(duration + " seconds for " + repeat + " iteration: " + repeat / 1000.0 / duration + " Kcalls/sec");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment