fork(1) download
  1. import java.io.Closeable;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.PrintWriter;
  8. import java.util.NoSuchElementException;
  9. import java.util.Scanner;
  10.  
  11. public class Main {
  12. public static void main(String[] args) {
  13. test("10000000.txt",10000000,1,1,1000000000);
  14. }
  15. public static void test(String filename,int h,int w,int min,int max) {
  16. try {
  17. generate(filename, h, w, min, max);
  18. bm1(filename);
  19. bm2(filename);
  20. bm3(filename);
  21. } catch (IOException e) {
  22. e.printStackTrace();
  23. }
  24. }
  25. public static void generate(String filename,int h,int w,int min,int max) {
  26. try(PrintWriter pw = new PrintWriter(filename)) {
  27. pw.println(h + " " + w);
  28. for(int i=0;i<h;i++) {
  29. for(int j=0;j<w;j++) {
  30. if (j > 0) {
  31. pw.print(' ');
  32. }
  33. pw.print(randomInt(min, max));
  34. }
  35. pw.println();
  36. }
  37. } catch (FileNotFoundException e) {
  38. e.printStackTrace();
  39. }
  40. }
  41. public static int randomInt(int min,int max) {
  42. return (int) (Math.random() * (max - min + 1)) + min;
  43. }
  44. public static void bm1(String filename) throws FileNotFoundException {
  45. long stime = System.nanoTime();
  46. Scanner sc = new Scanner(new File(filename));
  47. int h = sc.nextInt();
  48. int w = sc.nextInt();
  49. long sum = 0;
  50. for(int i=0;i<h;i++) {
  51. for(int j=0;j<w;j++) {
  52. sum += sc.nextInt();
  53. }
  54. }
  55. System.out.println(sum);
  56. System.out.println((System.nanoTime() - stime) / 1000000 + " ms");
  57. sc.close();
  58. }
  59. public static void bm2(String filename) throws FileNotFoundException {
  60. long stime = System.nanoTime();
  61. Scanner sc = new Scanner(new File(filename));
  62. int h = Integer.parseInt(sc.next());
  63. int w = Integer.parseInt(sc.next());
  64. long sum = 0;
  65. for(int i=0;i<h;i++) {
  66. for(int j=0;j<w;j++) {
  67. sum += Integer.parseInt(sc.next());
  68. }
  69. }
  70. System.out.println(sum);
  71. System.out.println((System.nanoTime() - stime) / 1000000 + " ms");
  72. sc.close();
  73. }
  74. public static void bm3(String filename) throws FileNotFoundException {
  75. long stime = System.nanoTime();
  76. FastScanner sc = new FastScanner(new FileInputStream(filename));
  77. int h = sc.nextInt();
  78. int w = sc.nextInt();
  79. long sum = 0;
  80. for(int i=0;i<h;i++) {
  81. for(int j=0;j<w;j++) {
  82. sum += sc.nextInt();
  83. }
  84. }
  85. System.out.println(sum);
  86. System.out.println((System.nanoTime() - stime) / 1000000 + " ms");
  87. sc.close();
  88. }
  89. }
  90. class FastScanner implements Closeable {
  91. private final InputStream in;
  92. private final byte[] buffer = new byte[1024];
  93. private int ptr = 0;
  94. private int buflen = 0;
  95. public FastScanner(InputStream in) {
  96. this.in = in;
  97. }
  98. private boolean hasNextByte() {
  99. if (ptr < buflen) {
  100. return true;
  101. }else{
  102. ptr = 0;
  103. try {
  104. buflen = in.read(buffer);
  105. } catch (IOException e) {
  106. e.printStackTrace();
  107. }
  108. if (buflen <= 0) {
  109. return false;
  110. }
  111. }
  112. return true;
  113. }
  114. private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;}
  115. private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;}
  116. public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte();}
  117. public String next() {
  118. if (!hasNext()) throw new NoSuchElementException();
  119. StringBuilder sb = new StringBuilder();
  120. int b = readByte();
  121. while(isPrintableChar(b)) {
  122. sb.appendCodePoint(b);
  123. b = readByte();
  124. }
  125. return sb.toString();
  126. }
  127. public long nextLong() {
  128. if (!hasNext()) throw new NoSuchElementException();
  129. long n = 0;
  130. boolean minus = false;
  131. int b = readByte();
  132. if (b == '-') {
  133. minus = true;
  134. b = readByte();
  135. }
  136. if (b < '0' || '9' < b) {
  137. throw new NumberFormatException();
  138. }
  139. while(true){
  140. if ('0' <= b && b <= '9') {
  141. n *= 10;
  142. n += b - '0';
  143. }else if(b == -1 || !isPrintableChar(b)){
  144. return minus ? -n : n;
  145. }else{
  146. throw new NumberFormatException();
  147. }
  148. b = readByte();
  149. }
  150. }
  151. public int nextInt() {
  152. long nl = nextLong();
  153. if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException();
  154. return (int) nl;
  155. }
  156. public double nextDouble() { return Double.parseDouble(next());}
  157. public void close() {
  158. try {
  159. in.close();
  160. } catch (IOException e) {
  161. }
  162. }
  163. }
Success #stdin #stdout #stderr 0.12s 320576KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
java.io.FileNotFoundException: 10000000.txt (Permission denied)
	at java.io.FileOutputStream.open0(Native Method)
	at java.io.FileOutputStream.open(FileOutputStream.java:270)
	at java.io.FileOutputStream.<init>(FileOutputStream.java:213)
	at java.io.FileOutputStream.<init>(FileOutputStream.java:101)
	at java.io.PrintWriter.<init>(PrintWriter.java:184)
	at Main.generate(Main.java:26)
	at Main.test(Main.java:17)
	at Main.main(Main.java:13)
java.io.FileNotFoundException: 10000000.txt (No such file or directory)
	at java.io.FileInputStream.open0(Native Method)
	at java.io.FileInputStream.open(FileInputStream.java:195)
	at java.io.FileInputStream.<init>(FileInputStream.java:138)
	at java.util.Scanner.<init>(Scanner.java:611)
	at Main.bm1(Main.java:46)
	at Main.test(Main.java:18)
	at Main.main(Main.java:13)