Skip to content

Instantly share code, notes, and snippets.

@megascus
Created May 7, 2017 15:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save megascus/04cab0e0c90d640c2e575e69c77546c4 to your computer and use it in GitHub Desktop.
Save megascus/04cab0e0c90d640c2e575e69c77546c4 to your computer and use it in GitHub Desktop.
diff InputStream and Reader
package test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import static java.nio.charset.StandardCharsets.*;
public class Test {
private static final String lessThan2G = "D:\\lessThan2G.txt";
private static final String moreThan2G = "D:\\moreThan2G.txt";
static final String str = "あいうえおかきくけこさしすせそたちつてと\r\n";
static final int charlen = str.getBytes(UTF_8).length;
public static void main(String[] args) throws IOException {
System.out.println(System.getProperty("java.version"));
System.out.println("inputstream");
for (int i = 0; i < 10; i++) {
measureInputStream(lessThan2G);
}
System.out.println("inputstreamToString");
for (int i = 0; i < 10; i++) {
measureInputStreamToString(lessThan2G);
}
System.out.println("reader");
for (int i = 0; i < 10; i++) {
measureReader(lessThan2G);
}
}
public static void measureReader(String fileName) throws IOException {
long start = System.nanoTime();
BufferedReader reader = Files.newBufferedReader(Paths.get(fileName));
while (reader.readLine() != null) {
}
long end = System.nanoTime();
System.out.println((end - start) / 1000000 + "ms");
}
public static void measureInputStreamToString(String fileName) throws IOException {
long start = System.nanoTime();
InputStream inputStream = Files.newInputStream(Paths.get(fileName));
byte[] buff = new byte[8192];
while (inputStream.read(buff) == 8192) {
new String(buff);
}
long end = System.nanoTime();
System.out.println((end - start) / 1000000 + "ms");
}
public static void measureInputStream(String fileName) throws IOException {
long start = System.nanoTime();
InputStream inputStream = Files.newInputStream(Paths.get(fileName));
byte[] buff = new byte[8192];
while (inputStream.read(buff) == 8192) {
}
long end = System.nanoTime();
System.out.println((end - start) / 1000000 + "ms");
}
}
@megascus
Copy link
Author

megascus commented May 7, 2017

inputstream
988ms
929ms
917ms
923ms
917ms
901ms
897ms
935ms
910ms
892ms
inputstreamToString
5033ms
4918ms
4895ms
5160ms
4900ms
4942ms
4890ms
4882ms
4887ms
5159ms
reader
7397ms
7308ms
7066ms
7108ms
7321ms
7030ms
7041ms
7018ms
7316ms
7005ms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment