Skip to content

Instantly share code, notes, and snippets.

@t3hk0d3
Created June 28, 2012 05:00
Show Gist options
  • Save t3hk0d3/3009212 to your computer and use it in GitHub Desktop.
Save t3hk0d3/3009212 to your computer and use it in GitHub Desktop.
Iterations: 10000
Replace() formatter (20000) total time: 1412 ms (avg: 0.1412 ms)
Placeholder time: 1215 ms (avg: 0.1215 ms)
Replace time: 197 ms (avg: 0.0197 ms)
Compiled formatter (20000) total time: 1267 ms (avg: 0.1267 ms)
Placeholder time: 1266 ms (avg: 0.1266 ms)
Replace time: 1 ms (avg: 1.0E-4 ms)
Code:
MessageFormat format = SimpleMessageFormat.compile("<%player> %message");
int iterations = 10000;
/**
* Replace() test
*/
PlaceholderManager.runCalls = 0;
String message = "<%player> %message";
long start = System.currentTimeMillis();
for (int i = 0 ; i < iterations ; i++) {
message.replace("%player", manager.run("player", null, this.message)).replace("%message", manager.run("message", null, this.message));
}
long result2 = System.currentTimeMillis() - start;
long runCalls2 = PlaceholderManager.runCalls;
long runTime2 = PlaceholderManager.runTime;
PlaceholderManager.runCalls = 0;
/**
* Compiled test
*/
PlaceholderManager.runTime = 0;
start = System.currentTimeMillis();
for (int i = 0 ; i < iterations ; i++) {
format.format(this.message, manager);
}
long result1 = System.currentTimeMillis() - start;
long runCalls1 = PlaceholderManager.runCalls;
long runTime1 = PlaceholderManager.runTime;
System.out.println("Iterations: " + iterations);
System.out.println("Replace() formatter ("+runCalls2+") total time: " + result2 + " ms (avg: " + ((float)result2/iterations) + " ms)");
System.out.println(" Placeholder time: " + runTime2 + " ms (avg: " + ((float)runTime2/iterations) + " ms)");
System.out.println(" Replace time: " + (result2 - runTime2) + " ms (avg: " + ((float)(result2 - runTime2)/iterations) + " ms)");
System.out.println("Compiled formatter ("+runCalls1+") total time: " + result1 + " ms (avg: " + ((float)result1/iterations) + " ms)");
System.out.println(" Placeholder time: " + runTime1 + " ms (avg: " + ((float)runTime1/iterations) + " ms)");
System.out.println(" Replace time: " + (result1 - runTime1) + " ms (avg: " + ((float)(result1 - runTime1)/iterations) + " ms)");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment