fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10.  
  11. private final static int ITERATIONS = 1000000;
  12. private final static String THE_STRING = "A STRING!";
  13.  
  14. public static void main (String[] args) throws java.lang.Exception
  15. {
  16. for (int pass=0; pass<10; ++pass) {
  17. long startTime = System.nanoTime();
  18. for (int i = 0; i < ITERATIONS; i++) {
  19. withTemp();
  20. }
  21. long endTime = System.nanoTime();
  22. long duration1 = (endTime - startTime);
  23.  
  24. startTime = System.nanoTime();
  25. for (int i = 0; i < ITERATIONS; i++) {
  26. noTemp();
  27. }
  28. endTime = System.nanoTime();
  29. long duration2 = (endTime - startTime);
  30.  
  31. System.out.println("noTemp() "+(duration2/1000000d)+" ms.");
  32. System.out.println("withTemp() "+(duration1/1000000d)+" ms.");
  33. }
  34. }
  35.  
  36. public static void noTemp() {
  37. doSomething(getString());
  38. doSomething(getString());
  39. }
  40.  
  41. public static void withTemp() {
  42. String s = getString();
  43. doSomething(s);
  44. doSomething(s);
  45. }
  46.  
  47. public static String getString() { return THE_STRING; }
  48. public static void doSomething(String s){}
  49.  
  50.  
  51. }
Success #stdin #stdout 0.11s 380224KB
stdin
Standard input is empty
stdout
noTemp() 1.370099 ms.
withTemp() 3.018705 ms.
noTemp() 1.369754 ms.
withTemp() 0.926916 ms.
noTemp() 1.381648 ms.
withTemp() 0.937573 ms.
noTemp() 1.38378 ms.
withTemp() 0.921087 ms.
noTemp() 1.36953 ms.
withTemp() 0.921061 ms.
noTemp() 1.36959 ms.
withTemp() 0.926409 ms.
noTemp() 1.379429 ms.
withTemp() 0.920986 ms.
noTemp() 1.369524 ms.
withTemp() 0.918172 ms.
noTemp() 1.392308 ms.
withTemp() 0.907474 ms.
noTemp() 1.369506 ms.
withTemp() 0.921054 ms.