• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.jsoup.integration;
2 
3 import java.util.Date;
4 
5 /**
6  Does an A/B test on two methods, and prints out how long each took.
7 
8  @author Jonathan Hedley, jonathan@hedley.net */
9 public class Benchmark {
run(Runnable a, Runnable b, int count)10     public static void run(Runnable a, Runnable b, int count) {
11         long aMillis;
12         long bMillis;
13 
14         print("Running test A (x%d)", count);
15         aMillis = time(a, count);
16         print("Running test B");
17         bMillis = time(b, count);
18 
19         print("\nResults:");
20         print("A: %.2fs", aMillis / 1000f);
21         print("B: %.2fs", bMillis / 1000f);
22         print("\nB ran in %.2f %% time of A\n", (bMillis *1f / aMillis * 1f) * 100f);
23     }
24 
time(Runnable test, int count)25     private static long time(Runnable test, int count) {
26         Date start = new Date();
27         for (int i = 0; i < count; i++) {
28             test.run();
29         }
30         Date end = new Date();
31         return end.getTime() - start.getTime();
32     }
33 
print(String msgFormat, Object... msgParams)34     private static void print(String msgFormat, Object... msgParams) {
35         System.out.println(String.format(msgFormat, msgParams));
36     }
37 }
38