fork download
  1.  
  2. import java.util.*;
  3. import java.lang.*;
  4.  
  5. class Main
  6. {
  7. public static void main (String[] args) throws java.lang.Exception
  8. {
  9. new Main();
  10. }
  11.  
  12. public Main() {
  13.  
  14. long a = System.currentTimeMillis();
  15. int n = 0;
  16. for (int i=0; i<1000000; i++) {
  17. synchronized(this) {
  18. n += 5*5;
  19. }
  20. }
  21. System.out.println("1M computations: " + (System.currentTimeMillis()-a)/1000D+"s");
  22.  
  23. a = System.currentTimeMillis();
  24. synchronized(this) {
  25. for (int i=0; i<1000000; i++) {
  26. n += 5*5;
  27. }
  28. }
  29. System.out.println("1M synchronized code blocks: " + (System.currentTimeMillis()-a)/1000D+"s");
  30.  
  31. a = System.currentTimeMillis();
  32. n = 0;
  33. for (int i=0; i<1000000; i++) {
  34. n += poti();
  35. }
  36. System.out.println("1M synchronized method calls: " + (System.currentTimeMillis()-a)/1000D+"s");
  37. }
  38.  
  39. public synchronized int poti()
  40. {
  41. return 5*5;
  42. }
  43. }
  44.  
Success #stdin #stdout 0.08s 245760KB
stdin
Standard input is empty
stdout
1M computations: 0.027s
1M synchronized code blocks: 0.0010s
1M synchronized method calls: 0.024s