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. class Hoge
  8. {
  9. public static int n = 0;
  10. public static int m = 0;
  11. public static int h = 0;
  12. }
  13.  
  14. /* Name of the class has to be "Main" only if the class is public. */
  15. class Ideone
  16. {
  17. static final int NUM = 15;
  18.  
  19. static Foo[] sFoos;
  20.  
  21. Foo[] iFoos;
  22.  
  23. public static void main (String[] args) throws java.lang.Exception
  24. {
  25. Ideone io = new Ideone();
  26.  
  27. io.test();
  28. }
  29.  
  30. void test() {
  31. long t0, t1;
  32.  
  33. Ideone io = new Ideone();
  34.  
  35. Random rand = new Random();
  36.  
  37. Foo[] foos = new Foo[NUM];
  38. foos[0] = new Foo0();
  39. foos[1] = new Foo1();
  40. foos[2] = new Foo2();
  41. foos[3] = new Foo3();
  42. foos[4] = new Foo4();
  43. foos[5] = new Foo5();
  44. foos[6] = new Foo6();
  45. foos[7] = new Foo7();
  46. foos[8] = new Foo8();
  47. foos[9] = new Foo9();
  48. foos[10] = new Foo10();
  49.  
  50. for (int i = 11; i < NUM; i++) {
  51. foos[i] = foos[0];
  52. }
  53.  
  54. sFoos = iFoos = io.iFoos = foos;
  55.  
  56. int[] indexes = new int[20000000];
  57. for (int i = 0; i < indexes.length; i++) {
  58. indexes[i] = rand.nextInt(NUM);
  59. }
  60.  
  61. ///////////////////////////////////////////
  62.  
  63. System.out.println("Local Variable Array");
  64. t0 = System.currentTimeMillis();
  65. for (int i = 0; i < indexes.length; i++) {
  66. foos[indexes[i]].doit();
  67. }
  68. t1 = System.currentTimeMillis();
  69. System.out.println(t1 + "-" + t0 + "=" + (t1 - t0));
  70.  
  71. ///////////////////////////////////////////
  72.  
  73. System.out.println("Class Field Array"); Hoge.n = 0;
  74. t0 = System.currentTimeMillis();
  75. for (int i = 0; i < indexes.length; i++) {
  76. sFoos[indexes[i]].doit();
  77. }
  78. t1 = System.currentTimeMillis();
  79. System.out.println(t1 + "-" + t0 + "=" + (t1 - t0));
  80.  
  81. ///////////////////////////////////////////
  82.  
  83. System.out.println("Instance(this) Field Array"); Hoge.n = 0;
  84. t0 = System.currentTimeMillis();
  85. for (int i = 0; i < indexes.length; i++) {
  86. iFoos[indexes[i]].doit();
  87. }
  88. t1 = System.currentTimeMillis();
  89. System.out.println(t1 + "-" + t0 + "=" + (t1 - t0));
  90.  
  91. ///////////////////////////////////////////
  92.  
  93. System.out.println("Instance(local) Field Array"); Hoge.n = 0;
  94. t0 = System.currentTimeMillis();
  95. for (int i = 0; i < indexes.length; i++) {
  96. io.iFoos[indexes[i]].doit();
  97. }
  98. t1 = System.currentTimeMillis();
  99. System.out.println(t1 + "-" + t0 + "=" + (t1 - t0));
  100.  
  101. ///////////////////////////////////////////
  102.  
  103. System.out.println("tableswitch");
  104. t0 = System.currentTimeMillis();
  105. for (int i = 0; i < indexes.length; i++) {
  106. switch (indexes[i]) {
  107. case 0:
  108. case 11:
  109. case 12:
  110. case 13:
  111. case 14:
  112. Hoge.m++;
  113. break;
  114. case 1:
  115. Hoge.m = (Hoge.m + 1) * 2;
  116. break;
  117. case 2:
  118. Hoge.m += 13;
  119. break;
  120. case 3:
  121. Hoge.m -= 4;
  122. break;
  123. case 4:
  124. Hoge.m *= 3;
  125. break;
  126. case 5:
  127. Hoge.m += Hoge.m % 17;
  128. break;
  129. case 6:
  130. Hoge.m /= 100;
  131. break;
  132. case 7:
  133. Hoge.m--;
  134. break;
  135. case 8:
  136. Hoge.m += 30;
  137. break;
  138. case 9:
  139. Hoge.m >>= 1;
  140. break;
  141. case 10:
  142. Hoge.m -= Hoge.m % 73;
  143. break;
  144. }
  145. }
  146. t1 = System.currentTimeMillis();
  147. System.out.println(t1 + "-" + t0 + "=" + (t1 - t0));
  148.  
  149. ///////////////////////////////////////////
  150.  
  151. for (int i = 0; i < indexes.length; i++) {
  152. switch (indexes[i]) {
  153. case 0: indexes[i] = 5; break;
  154. case 1: indexes[i] = 101; break;
  155. case 2: indexes[i] = 93; break;
  156. case 3: indexes[i] = 4016; break;
  157. case 4: indexes[i] = 880888; break;
  158. case 5: indexes[i] = 41; break;
  159. case 6: indexes[i] = 22; break;
  160. case 7: indexes[i] = 330; break;
  161. case 8: indexes[i] = 1010; break;
  162. case 9: indexes[i] = 171; break;
  163. case 10: indexes[i] = 999; break;
  164. case 11: indexes[i] = 10010; break;
  165. case 12: indexes[i] = 7521; break;
  166. case 13: indexes[i] = 8; break;
  167. case 14: indexes[i] = 76; break;
  168. }
  169. }
  170.  
  171. ///////////////////////////////////////////
  172.  
  173. System.out.println("lookupswitch");
  174. t0 = System.currentTimeMillis();
  175. for (int i = 0; i < indexes.length; i++) {
  176. switch (indexes[i]) {
  177. case 5:
  178. case 10010:
  179. case 7521:
  180. case 8:
  181. case 76:
  182. Hoge.h++;
  183. break;
  184. case 101:
  185. Hoge.h = (Hoge.h + 1) * 2;
  186. break;
  187. case 93:
  188. Hoge.h += 13;
  189. break;
  190. case 4016:
  191. Hoge.h -= 4;
  192. break;
  193. case 880888:
  194. Hoge.h *= 3;
  195. break;
  196. case 41:
  197. Hoge.h += Hoge.h % 17;
  198. break;
  199. case 22:
  200. Hoge.h /= 100;
  201. break;
  202. case 330:
  203. Hoge.h--;
  204. break;
  205. case 1010:
  206. Hoge.h += 30;
  207. break;
  208. case 171:
  209. Hoge.h >>= 1;
  210. break;
  211. case 999:
  212. Hoge.h -= Hoge.h % 73;
  213. break;
  214. }
  215. }
  216. t1 = System.currentTimeMillis();
  217. System.out.println(t1 + "-" + t0 + "=" + (t1 - t0));
  218.  
  219. ///////////////////////////////////////////
  220.  
  221. System.out.println("check");
  222.  
  223. System.out.println(Hoge.n);
  224. System.out.println(Hoge.m);
  225. System.out.println(Hoge.h);
  226. }
  227. }
  228.  
  229. interface Foo
  230. {
  231. void doit();
  232. }
  233.  
  234. class Foo0 implements Foo
  235. {
  236. public void doit() { Hoge.n++; }
  237. }
  238.  
  239. class Foo1 implements Foo
  240. {
  241. public void doit() { Hoge.n = (Hoge.n + 1) * 2; }
  242. }
  243.  
  244. class Foo2 implements Foo
  245. {
  246. public void doit() { Hoge.n += 13; }
  247. }
  248.  
  249. class Foo3 implements Foo
  250. {
  251. public void doit() { Hoge.n -= 4; }
  252. }
  253.  
  254. class Foo4 implements Foo
  255. {
  256. public void doit() { Hoge.n *= 3; }
  257. }
  258.  
  259. class Foo5 implements Foo
  260. {
  261. public void doit() { Hoge.n += Hoge.n % 17; }
  262. }
  263.  
  264. class Foo6 implements Foo
  265. {
  266. public void doit() { Hoge.n /= 100; }
  267. }
  268.  
  269. class Foo7 implements Foo
  270. {
  271. public void doit() { Hoge.n--; }
  272. }
  273.  
  274. class Foo8 implements Foo
  275. {
  276. public void doit() { Hoge.n += 30; }
  277. }
  278.  
  279. class Foo9 implements Foo
  280. {
  281. public void doit() { Hoge.n >>= 1; }
  282. }
  283.  
  284. class Foo10 implements Foo
  285. {
  286. public void doit() { Hoge.n -= Hoge.n % 73; }
  287. }
Success #stdin #stdout 4.12s 380864KB
stdin
Standard input is empty
stdout
Local Variable Array
1414263745841-1414263745367=474
Class Field Array
1414263746311-1414263745842=469
Instance(this) Field Array
1414263746792-1414263746312=480
Instance(local) Field Array
1414263747271-1414263746793=478
tableswitch
1414263747597-1414263747271=326
lookupswitch
1414263748200-1414263747863=337
check
120
120
120