Skip to content

Commit 4831739

Browse files
committed
Measure IO solutions.
Results are definitely not surprising: Measuring solutions with file io/files/numbers-1000000x1.txt: 0.10318 +- 0.00177 seconds time elapsed ( +- 1.71% ) for InputReaderSolution 0.165462 +- 0.000919 seconds time elapsed ( +- 0.56% ) for StringTokenizerSolution 0.73153 +- 0.00274 seconds time elapsed ( +- 0.37% ) for ScannerSolution Measuring solutions with file io/files/numbers-10000x100.txt: 0.10104 +- 0.00196 seconds time elapsed ( +- 1.94% ) for InputReaderSolution 0.17065 +- 0.00143 seconds time elapsed ( +- 0.84% ) for StringTokenizerSolution 0.73497 +- 0.00158 seconds time elapsed ( +- 0.21% ) for ScannerSolution Measuring solutions with file io/files/numbers-1000x1000.txt: 0.09826 +- 0.00190 seconds time elapsed ( +- 1.93% ) for InputReaderSolution 0.18647 +- 0.00171 seconds time elapsed ( +- 0.92% ) for StringTokenizerSolution 0.74256 +- 0.00178 seconds time elapsed ( +- 0.24% ) for ScannerSolution Measuring solutions with file io/files/numbers-1000x100.txt: 0.04601 +- 0.00113 seconds time elapsed ( +- 2.45% ) for InputReaderSolution 0.11440 +- 0.00184 seconds time elapsed ( +- 1.61% ) for StringTokenizerSolution 0.20467 +- 0.00132 seconds time elapsed ( +- 0.65% ) for ScannerSolution Measuring solutions with file io/files/numbers-1000x1.txt: 0.04611 +- 0.00109 seconds time elapsed ( +- 2.36% ) for InputReaderSolution 0.04982 +- 0.00169 seconds time elapsed ( +- 3.38% ) for StringTokenizerSolution 0.11533 +- 0.00175 seconds time elapsed ( +- 1.52% ) for ScannerSolution Measuring solutions with file io/files/numbers-100x10000.txt: 0.10206 +- 0.00189 seconds time elapsed ( +- 1.85% ) for InputReaderSolution 0.17685 +- 0.00193 seconds time elapsed ( +- 1.09% ) for StringTokenizerSolution 0.74295 +- 0.00200 seconds time elapsed ( +- 0.27% ) for ScannerSolution Measuring solutions with file io/files/numbers-100x1000.txt: 0.044376 +- 0.000827 seconds time elapsed ( +- 1.86% ) for InputReaderSolution 0.11756 +- 0.00149 seconds time elapsed ( +- 1.27% ) for StringTokenizerSolution 0.204209 +- 0.000818 seconds time elapsed ( +- 0.40% ) for ScannerSolution Measuring solutions with file io/files/numbers-1x1000000.txt: 0.10208 +- 0.00178 seconds time elapsed ( +- 1.74% ) for InputReaderSolution 0.19291 +- 0.00173 seconds time elapsed ( +- 0.90% ) for StringTokenizerSolution 0.74970 +- 0.00214 seconds time elapsed ( +- 0.29% ) for ScannerSolution Measuring solutions with file io/files/numbers-1x1000.txt: 0.04064 +- 0.00111 seconds time elapsed ( +- 2.74% ) for InputReaderSolution 0.06048 +- 0.00186 seconds time elapsed ( +- 3.08% ) for StringTokenizerSolution 0.11580 +- 0.00166 seconds time elapsed ( +- 1.44% ) for ScannerSolution Also fix the templates, lol.
1 parent 91a402e commit 4831739

12 files changed

+342
-4
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
.out
22
tuenti/*
3+
*.class
4+
io/files/*
5+
icpcmasters2021/*

.idea/algorithms.iml

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/libraries/gson_2_8_2.xml

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

io/FileGenerator.java

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import java.io.BufferedOutputStream;
2+
import java.io.FileOutputStream;
3+
import java.io.IOException;
4+
import java.io.PrintWriter;
5+
6+
public class FileGenerator {
7+
public static void main(String[] args) throws IOException {
8+
printNumbers(1,1_000_000);
9+
printNumbers(1_000_000,1);
10+
printNumbers(100,10_000);
11+
printNumbers(10_000,100);
12+
printNumbers(100,1_000);
13+
printNumbers(1_000,100);
14+
printNumbers(1_000,1);
15+
printNumbers(1,1_000);
16+
printNumbers(1_000,1_000);
17+
}
18+
19+
private static void printNumbers(int rows, int cols) throws IOException {
20+
try (PrintWriter out = new PrintWriter(new BufferedOutputStream(new FileOutputStream(
21+
String.format("io/files/numbers-%sx%s.txt", rows, cols))))) {
22+
out.println(rows * cols);
23+
for (int i = 0; i < rows; i++) {
24+
for (int j = 0; j < cols; j++) {
25+
if (j > 0) {
26+
out.print(' ');
27+
}
28+
out.print(i*cols + j);
29+
}
30+
out.println();
31+
}
32+
out.flush();
33+
}
34+
}
35+
}

io/InputReaderSolution.java

+157
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
import java.io.BufferedOutputStream;
2+
import java.io.IOException;
3+
import java.io.InputStream;
4+
import java.io.PrintWriter;
5+
import java.util.InputMismatchException;
6+
7+
public class InputReaderSolution implements Runnable {
8+
9+
private static void solve() {
10+
long sum = 0;
11+
int n = in.readInt();
12+
while (n-- > 0) {
13+
sum += in.readInt();
14+
}
15+
out.println(sum);
16+
}
17+
18+
/** Template starts: */
19+
@Override
20+
public void run() {
21+
solve();
22+
out.flush();
23+
System.exit(0);
24+
}
25+
26+
private final static InputReader in = new InputReader(System.in);
27+
private final static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
28+
29+
public static void main(String[] args) {
30+
new Thread(null, new InputReaderSolution(), "Main", 1 << 26).start();
31+
}
32+
33+
private static class InputReader {
34+
private final InputStream stream;
35+
private final byte[] buf = new byte[1024];
36+
private int curChar;
37+
private int numChars;
38+
39+
InputReader(InputStream stream) {
40+
this.stream = stream;
41+
}
42+
43+
private byte read() {
44+
if (numChars == -1) {
45+
throw new InputMismatchException("EOF has been reached");
46+
}
47+
if (curChar >= numChars) {
48+
curChar = 0;
49+
try {
50+
numChars = stream.read(buf);
51+
} catch (IOException e) {
52+
throw new InputMismatchException();
53+
}
54+
if (numChars <= 0)
55+
return -1;
56+
}
57+
return buf[curChar++];
58+
}
59+
60+
int readInt() {
61+
byte c = read();
62+
while (isWhitespace(c)) {
63+
c = read();
64+
}
65+
if (isEOF(c)) {
66+
throw new InputMismatchException("EOF has been reached");
67+
}
68+
69+
int sgn = 1;
70+
if (c == '-') {
71+
sgn = -1;
72+
c = read();
73+
}
74+
int res = 0;
75+
do {
76+
if (c < '0' || c > '9') {
77+
throw new InputMismatchException();
78+
}
79+
if (res > Integer.MAX_VALUE / 10) {
80+
throw new InputMismatchException("Input is not an integer");
81+
}
82+
res *= 10;
83+
int d = c - '0';
84+
if (res > Integer.MAX_VALUE - d) {
85+
throw new InputMismatchException("Input is not an integer");
86+
}
87+
res += d;
88+
c = read();
89+
} while (!isWhitespaceOrEOF(c));
90+
return res * sgn;
91+
}
92+
93+
long readLong() {
94+
byte c = read();
95+
while (isWhitespace(c)) {
96+
c = read();
97+
}
98+
if (isEOF(c)) {
99+
throw new InputMismatchException("EOF has been reached");
100+
}
101+
102+
int sgn = 1;
103+
if (c == '-') {
104+
sgn = -1;
105+
c = read();
106+
}
107+
long res = 0;
108+
do {
109+
if (c < '0' || c > '9') {
110+
throw new InputMismatchException();
111+
}
112+
if (res > Long.MAX_VALUE / 10) {
113+
throw new InputMismatchException("Input is not a long integer");
114+
}
115+
res *= 10;
116+
int d = c - '0';
117+
if (res > Long.MAX_VALUE - d) {
118+
throw new InputMismatchException("Input is not a long integer");
119+
}
120+
res += d;
121+
c = read();
122+
} while (!isWhitespaceOrEOF(c));
123+
return res * sgn;
124+
}
125+
126+
String readString() {
127+
byte c = read();
128+
while (isWhitespace(c)) {
129+
c = read();
130+
}
131+
if (isEOF(c)) {
132+
return null;
133+
}
134+
135+
StringBuilder res = new StringBuilder();
136+
do {
137+
if (Character.isValidCodePoint(c)) {
138+
res.appendCodePoint(c);
139+
}
140+
c = read();
141+
} while (!isWhitespaceOrEOF(c));
142+
return res.toString();
143+
}
144+
145+
private boolean isWhitespace(byte c) {
146+
return c == ' ' || c == '\n' || c == '\r' || c == '\t';
147+
}
148+
149+
private boolean isEOF(byte c) {
150+
return c == -1;
151+
}
152+
153+
private boolean isWhitespaceOrEOF(byte c) {
154+
return isWhitespace(c) || isEOF(c);
155+
}
156+
}
157+
}

io/ScannerSolution.java

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import java.io.BufferedOutputStream;
2+
import java.io.InputStreamReader;
3+
import java.io.PrintWriter;
4+
import java.util.Scanner;
5+
6+
public class ScannerSolution implements Runnable {
7+
8+
private static void solve() {
9+
long sum = 0;
10+
int n = in.nextInt();
11+
while (n-- > 0) {
12+
sum += in.nextInt();
13+
}
14+
out.println(sum);
15+
}
16+
17+
/** Template starts: */
18+
@Override
19+
public void run() {
20+
solve();
21+
out.flush();
22+
System.exit(0);
23+
}
24+
25+
private final static Scanner in = new Scanner(new InputStreamReader(System.in));
26+
private final static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
27+
28+
public static void main(String[] args) {
29+
new Thread(null, new ScannerSolution(), "Main", 1 << 26).start();
30+
}
31+
}

io/StringTokenizerSolution.java

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import java.io.*;
2+
import java.util.StringTokenizer;
3+
4+
public class StringTokenizerSolution implements Runnable {
5+
6+
private static void solve() {
7+
long sum = 0;
8+
int n = readInt();
9+
while (n-- > 0) {
10+
sum += readInt();
11+
}
12+
out.println(sum);
13+
}
14+
15+
/** Template starts: */
16+
@Override
17+
public void run() {
18+
solve();
19+
out.flush();
20+
System.exit(0);
21+
}
22+
23+
private final static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
24+
private static StringTokenizer stringTokenizer;
25+
private final static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
26+
27+
public static void main(String[] args) {
28+
new Thread(null, new StringTokenizerSolution(), "Main", 1 << 26).start();
29+
}
30+
31+
private static long readLong() {
32+
return Long.parseLong(read());
33+
}
34+
35+
private static int readInt() {
36+
return Integer.parseInt(read());
37+
}
38+
39+
private static String read() {
40+
while (stringTokenizer == null || !stringTokenizer.hasMoreTokens()) {
41+
String line = readLine();
42+
if (line == null) {
43+
return null;
44+
}
45+
stringTokenizer = new StringTokenizer(line);
46+
}
47+
return stringTokenizer.nextToken();
48+
}
49+
50+
private static String readLine() {
51+
try {
52+
return in.readLine();
53+
} catch (IOException e) {
54+
return null;
55+
}
56+
}
57+
}

io/measure.sh

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/bin/bash
2+
3+
compile() {
4+
rm io/*.class
5+
javac io/*.java
6+
}
7+
8+
measure_solution() {
9+
solution=$1
10+
input_file=$2
11+
output=$(perf stat -r 100 -B sh -c "java -cp io $solution < $input_file" 2>&1)
12+
runtime=$(echo "$output" | grep "time elapsed" | sed 's/^ *//g')
13+
echo "$runtime"
14+
}
15+
16+
measure_file() {
17+
input_file=$1
18+
declare -a solutions=("InputReaderSolution" "StringTokenizerSolution" "ScannerSolution")
19+
echo "Measuring solutions with file $input_file:"
20+
all_runtimes=
21+
for solution in "${solutions[@]}"; do
22+
runtime=$(measure_solution "$solution" "$input_file")
23+
all_runtimes=" $runtime for $solution
24+
$all_runtimes"
25+
done
26+
echo "$all_runtimes" | grep -v "^$" | sort
27+
echo
28+
}
29+
30+
# Check preconditions
31+
if (( EUID != 0 )); then
32+
echo "Please run as root (perf requires it)"
33+
exit
34+
fi
35+
36+
# Compile and measure performance
37+
compile
38+
for input_file in io/files/*.txt; do
39+
measure_file "$input_file"
40+
done

scratch/Main.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
import java.util.InputMismatchException;
66

77
public class Main implements Runnable {
8+
private void solve() {
89

9-
private static void solve() {
1010
}
1111

1212
/** Template starts: */
@@ -117,6 +117,10 @@ long readLong() {
117117
return res * sgn;
118118
}
119119

120+
double readDouble() {
121+
return Double.parseDouble(readString());
122+
}
123+
120124
String readString() {
121125
byte c = read();
122126
while (isWhitespace(c)) {

templates/InputReaderTemplate.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public void run() {
2121
private final static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
2222

2323
public static void main(String[] args) {
24-
new Thread(null, new InputReaderTemplate(), "Main", 1 << 26).start();
24+
new Thread(null, new InputReaderSolution(), "Main", 1 << 26).start();
2525
}
2626

2727
private static class InputReader {

templates/ScannerTemplate.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ public void run() {
2020
private final static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
2121

2222
public static void main(String[] args) {
23-
new Thread(null, new InputReaderTemplate(), "Main", 1 << 26).start();
23+
new Thread(null, new ScannerSolution(), "Main", 1 << 26).start();
2424
}
2525
}

templates/StringTokenizerTemplate.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public void run() {
1919
private final static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
2020

2121
public static void main(String[] args) {
22-
new Thread(null, new InputReaderTemplate(), "Main", 1 << 26).start();
22+
new Thread(null, new StringTokenizerSolution(), "Main", 1 << 26).start();
2323
}
2424

2525
private static long readLong() {

0 commit comments

Comments
 (0)