fork(2) download
  1. class PerformanceTest {
  2. private static class Singleton {
  3. static Singleton s;
  4. public static Singleton getInstance() {
  5. if (s == null)
  6. s = new Singleton();
  7. return s;
  8. }
  9. public int get() {
  10. return 9;
  11. }
  12. }
  13.  
  14. private static class InstanceRef {
  15. public int get() {
  16. return 9;
  17. }
  18. }
  19.  
  20. private static class Timer {
  21. protected long actualTime;
  22.  
  23. public void startTask() {
  24. actualTime = System.nanoTime();
  25. }
  26.  
  27. public long getCurrentTime() {
  28. long i = System.nanoTime() - actualTime;
  29. return i;
  30. }
  31.  
  32. public void printActualTime() {
  33. System.out.println(getCurrentTime());
  34. }
  35. }
  36.  
  37. public static void main(String[] args) {
  38. Timer t = new Timer();
  39. t.startTask();
  40. new InstanceRef().get();
  41. t.printActualTime();
  42.  
  43. Timer ti = new Timer();
  44. ti.startTask();
  45. Singleton.getInstance().get();
  46. ti.printActualTime();
  47. }
  48. }
Success #stdin #stdout 0.07s 380160KB
stdin
Standard input is empty
stdout
1366348
435425