fork(1) download
  1. import java.util.Random;
  2.  
  3. interface Sum {
  4. long sum(int[] buf);
  5. }
  6.  
  7. class RangeSum implements Sum {
  8. @Override
  9. public long sum(final int[] buf) {
  10. long sum = 0L;
  11. for (int v: buf) {
  12. sum += v;
  13. }
  14. return sum;
  15. }
  16. }
  17.  
  18. class IterSum implements Sum {
  19. @Override
  20. public long sum(final int[] buf) {
  21. long sum = 0L;
  22. for (int i = 0; i < buf.length; ++i) {
  23. sum += buf[i];
  24. }
  25. return sum;
  26. }
  27. }
  28.  
  29. class BackIterSum implements Sum {
  30. @Override
  31. public long sum(final int[] buf) {
  32. long sum = 0L;
  33. for (int i = buf.length; i-- > 0;) {
  34. sum += buf[i];
  35. }
  36. return sum;
  37. }
  38. }
  39.  
  40. public class Main {
  41. public static void main(final String[] args) {
  42. Compiler.compileClass(Main.class);
  43. Compiler.compileClass(RangeSum.class);
  44. Compiler.compileClass(IterSum.class);
  45. Compiler.compileClass(BackIterSum.class);
  46. int size = Integer.parseInt(args[0]);
  47. int[] buf = new int[size];
  48. Random random = new Random(0L);
  49. for (int i = 0; i < size; ++i) {
  50. buf[i] = random.nextInt();
  51. }
  52. Sum sum;
  53. switch (Integer.parseInt(args[1])) {
  54. case 0:
  55. sum = new RangeSum();
  56. break;
  57. case 1:
  58. sum = new IterSum();
  59. break;
  60. case 2:
  61. sum = new BackIterSum();
  62. break;
  63. default:
  64. throw new RuntimeException();
  65. }
  66. long start = System.currentTimeMillis();
  67. long total = sum.sum(buf);
  68. long end = System.currentTimeMillis();
  69. System.out.println(total + ": " + (end - start));
  70. }
  71. }
Runtime error #stdin #stdout #stderr 0.07s 380224KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
	at Main.main(Main.java:46)