fork(1) 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. long startTime = System.nanoTime();
  17. for (int i = 0; i < ITERATIONS; i++) {
  18. noTemp();
  19. }
  20. long endTime = System.nanoTime();
  21. long duration = (endTime - startTime);
  22.  
  23. startTime = System.nanoTime();
  24. for (int i = 0; i < ITERATIONS; i++) {
  25. withTemp();
  26. }
  27. endTime = System.nanoTime();
  28. long duration2 = (endTime - startTime);
  29.  
  30. if (duration < duration2) {
  31. System.out.println("noTemp() is faster by "+(duration2 - duration)/1000000d+" ms.");
  32. }
  33. else {
  34. System.out.println("withTemp() is faster by "+(duration - duration2)/1000000d+" ms.");
  35. }
  36.  
  37. }
  38.  
  39. public static void noTemp() {
  40. doSomething(getString());
  41. doSomething(getString());
  42. }
  43.  
  44. public static void withTemp() {
  45. String s = getString();
  46. doSomething(s);
  47. doSomething(s);
  48. }
  49.  
  50. public static String getString() { return THE_STRING; }
  51. public static void doSomething(String s){}
  52.  
  53.  
  54. }
Success #stdin #stdout 0.09s 380224KB
stdin
Standard input is empty
stdout
withTemp() is faster by 2.025071 ms.