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. 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. foo0();
  113. break;
  114. case 1:
  115. foo1();
  116. break;
  117. case 2:
  118. foo2();
  119. break;
  120. case 3:
  121. foo3();
  122. break;
  123. case 4:
  124. foo4();
  125. break;
  126. case 5:
  127. foo5();
  128. break;
  129. case 6:
  130. foo6();
  131. break;
  132. case 7:
  133. foo7();
  134. break;
  135. case 8:
  136. foo8();
  137. break;
  138. case 9:
  139. foo9();
  140. break;
  141. case 10:
  142. foo10();
  143. break;
  144. }
  145. }
  146. t1 = System.currentTimeMillis();
  147. System.out.println(t1 + "-" + t0 + "=" + (t1 - t0));
  148. Hoge.m = Hoge.h;
  149.  
  150. ///////////////////////////////////////////
  151.  
  152. for (int i = 0; i < indexes.length; i++) {
  153. switch (indexes[i]) {
  154. case 0: indexes[i] = 5; break;
  155. case 1: indexes[i] = 101; break;
  156. case 2: indexes[i] = 93; break;
  157. case 3: indexes[i] = 4016; break;
  158. case 4: indexes[i] = 880888; break;
  159. case 5: indexes[i] = 41; break;
  160. case 6: indexes[i] = 22; break;
  161. case 7: indexes[i] = 330; break;
  162. case 8: indexes[i] = 1010; break;
  163. case 9: indexes[i] = 171; break;
  164. case 10: indexes[i] = 999; break;
  165. case 11: indexes[i] = 10010; break;
  166. case 12: indexes[i] = 7521; break;
  167. case 13: indexes[i] = 8; break;
  168. case 14: indexes[i] = 76; break;
  169. }
  170. }
  171.  
  172. ///////////////////////////////////////////
  173.  
  174. System.out.println("lookupswitch"); Hoge.h = 0;
  175. t0 = System.currentTimeMillis();
  176. for (int i = 0; i < indexes.length; i++) {
  177. switch (indexes[i]) {
  178. case 5:
  179. case 10010:
  180. case 7521:
  181. case 8:
  182. case 76:
  183. foo0();
  184. break;
  185. case 101:
  186. foo1();
  187. break;
  188. case 93:
  189. foo2();
  190. break;
  191. case 4016:
  192. foo3();
  193. break;
  194. case 880888:
  195. foo4();
  196. break;
  197. case 41:
  198. foo5();
  199. break;
  200. case 22:
  201. foo6();
  202. break;
  203. case 330:
  204. foo7();
  205. break;
  206. case 1010:
  207. foo8();
  208. break;
  209. case 171:
  210. foo9();
  211. break;
  212. case 999:
  213. foo10();
  214. break;
  215. }
  216. }
  217. t1 = System.currentTimeMillis();
  218. System.out.println(t1 + "-" + t0 + "=" + (t1 - t0));
  219.  
  220. ///////////////////////////////////////////
  221.  
  222. System.out.println("check");
  223.  
  224. System.out.println(Hoge.n);
  225. System.out.println(Hoge.m);
  226. System.out.println(Hoge.h);
  227. }
  228.  
  229. void foo0() { Hoge.h++; }
  230. void foo1() { Hoge.h = (Hoge.h + 1) * 2; }
  231. void foo2() { Hoge.h += 13; }
  232. void foo3() { Hoge.h -= 4; }
  233. void foo4() { Hoge.h *= 3; }
  234. void foo5() { Hoge.h += Hoge.h % 17; }
  235. void foo6() { Hoge.h /= 100; }
  236. void foo7() { Hoge.h--; }
  237. void foo8() { Hoge.h += 30; }
  238. void foo9() { Hoge.h >>= 1; }
  239. void foo10() { Hoge.h -= Hoge.h % 73; }
  240. }
  241.  
  242. interface Foo
  243. {
  244. void doit();
  245. }
  246.  
  247. class Foo0 implements Foo
  248. {
  249. public void doit() { Hoge.n++; }
  250. }
  251.  
  252. class Foo1 implements Foo
  253. {
  254. public void doit() { Hoge.n = (Hoge.n + 1) * 2; }
  255. }
  256.  
  257. class Foo2 implements Foo
  258. {
  259. public void doit() { Hoge.n += 13; }
  260. }
  261.  
  262. class Foo3 implements Foo
  263. {
  264. public void doit() { Hoge.n -= 4; }
  265. }
  266.  
  267. class Foo4 implements Foo
  268. {
  269. public void doit() { Hoge.n *= 3; }
  270. }
  271.  
  272. class Foo5 implements Foo
  273. {
  274. public void doit() { Hoge.n += Hoge.n % 17; }
  275. }
  276.  
  277. class Foo6 implements Foo
  278. {
  279. public void doit() { Hoge.n /= 100; }
  280. }
  281.  
  282. class Foo7 implements Foo
  283. {
  284. public void doit() { Hoge.n--; }
  285. }
  286.  
  287. class Foo8 implements Foo
  288. {
  289. public void doit() { Hoge.n += 30; }
  290. }
  291.  
  292. class Foo9 implements Foo
  293. {
  294. public void doit() { Hoge.n >>= 1; }
  295. }
  296.  
  297. class Foo10 implements Foo
  298. {
  299. public void doit() { Hoge.n -= Hoge.n % 73; }
  300. }
Success #stdin #stdout 4.17s 380992KB
stdin
Standard input is empty
stdout
Local Variable Array
1414264900276-1414264899800=476
Class Field Array
1414264900748-1414264900276=472
Instance(this) Field Array
1414264901233-1414264900749=484
Instance(local) Field Array
1414264901721-1414264901234=487
tableswitch
1414264902048-1414264901721=327
lookupswitch
1414264902687-1414264902320=367
check
84
84
84