GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Java/ConvertInputStreamToStringBenchmark.java at main · Eggy115/Java Expand file tree/ Copy pathConvertInputStreamToStringBenchmark.javaMore file actions 210 lines (187 loc) · 8.14 KB / Copy pathConvertInputStreamToStringBenchmark.javaFile metadata and controls210 lines (187 loc) · 8.14 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 package other_examples; import com.google.common.base.Charsets; import com.google.common.io.CharStreams; import org.apache.commons.io.IOUtils; import org.openjdk.jmh.annotations.*; import org.openjdk.jmh.runner.Runner; import org.openjdk.jmh.runner.options.Options; import org.openjdk.jmh.runner.options.OptionsBuilder; import java.io.*; import java.nio.charset.StandardCharsets; import java.util.Scanner; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; /** * Created by vvedenin on 2/15/2016. */ @BenchmarkMode(Mode.AverageTime) @OutputTimeUnit(TimeUnit.MICROSECONDS) @Warmup(iterations = 2, time = 1, timeUnit = TimeUnit.SECONDS) @Measurement(iterations = 3, time = 1, timeUnit = TimeUnit.SECONDS) @Fork(1) @State(Scope.Benchmark) public class ConvertInputStreamToStringBenchmark { private final static String test1 = "test1847686128764812764876128пввпв76417826487216478216784621784672816478216784621784621786478216478216784261784621"+ "\n\r" +"782178647281647821647821697421687126784621874621786478216478216874"; private final static InputStream inputStream = IOUtils.toInputStream(test1, StandardCharsets.UTF_8); /* 1. Using IOUtils.toString (Apache Utils) */ @Benchmark public String test1_apacheToInputStream() throws IOException { mark(); String result = IOUtils.toString(inputStream, StandardCharsets.UTF_8); reset(); return result; } /* 2. Using CharStreams (guava) */ @Benchmark public String test2_guavaCharStreams() throws IOException { mark(); String result = CharStreams.toString(new InputStreamReader(inputStream, Charsets.UTF_8)); inputStream.reset(); return result; } /* 3. Using Scanner (JDK) */ @Benchmark public String test3_jdkScanner() throws IOException { mark(); Scanner s = new Scanner(inputStream).useDelimiter("\\A"); String result = s.hasNext() ? s.next() : ""; reset(); return result; } /* 4. Using Stream Api (Java 8) */ @Benchmark public String test4_jdkJava8() throws IOException { mark(); String result = new BufferedReader(new InputStreamReader(inputStream)).lines().collect(Collectors.joining("\n")); reset(); return result; } /* 5. Using parallel Stream Api (Java 8) */ @Benchmark public String test5_jdkJava8parallel() throws IOException { mark(); String result = new BufferedReader(new InputStreamReader(inputStream)).lines().parallel().collect(Collectors.joining("\n")); reset(); return result; } /* 6. Using InputStreamReader and StringBuilder (JDK) */ @Benchmark public String test6_inputStreamReaderAndStringBuilder() throws IOException { mark(); final int bufferSize = 1024; final char[] buffer = new char[bufferSize]; final StringBuilder out = new StringBuilder(); Reader in = new InputStreamReader(inputStream, "UTF-8"); for (; ; ) { int rsz = in.read(buffer, 0, buffer.length); if (rsz < 0) break; out.append(buffer, 0, rsz); } reset(); return out.toString(); } /* 7. Using StringWriter and IOUtils.copy (Apache Commons)*/ @Benchmark public String test7_apacheStringWriterAndIOUtilsCopy() throws IOException { mark(); StringWriter writer = new StringWriter(); IOUtils.copy(inputStream, writer, "UTF-8"); reset(); return writer.toString(); } /* 8. Using ByteArrayOutputStream and inputStream.read (JDK) */ @Benchmark public String test8_readByteArrayOutputStream() throws IOException { mark(); ByteArrayOutputStream result = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int length; while ((length = inputStream.read(buffer)) != -1) { result.write(buffer, 0, length); } reset(); return result.toString("UTF-8"); } /* 9. Using BufferedReader (JDK) */ @Benchmark public String test9_bufferedReaderReadLine() throws IOException { mark(); String newLine = System.getProperty("line.separator"); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); StringBuilder result = new StringBuilder(); String line; boolean flag = false; while ((line = reader.readLine()) != null) { result.append(flag? newLine: "").append(line); flag = true; } reset(); return result.toString(); } /* 10. Using BufferedInputStream and ByteArrayOutputStream (JDK) */ @Benchmark public String test10_bufferedInputStreamAndByteArrayOutputStream() throws IOException { mark(); BufferedInputStream bis = new BufferedInputStream(inputStream); ByteArrayOutputStream buf = new ByteArrayOutputStream(); int result = bis.read(); while(result != -1) { buf.write((byte) result); result = bis.read(); } reset(); return buf.toString(); } /* 11. Using inputStream.read() and StringBuilder (JDK) */ @Benchmark public String test11_inputStreamReadAndStringBuilder() throws IOException { mark(); int ch; StringBuilder sb = new StringBuilder(); while((ch = inputStream.read()) != -1) sb.append((char)ch); reset(); return sb.toString(); } /* 12. Using BufferedReader (JDK) */ @Benchmark public String test12_bufferedReaderReadLine2() throws IOException { mark(); String newLine = System.getProperty("line.separator"); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); StringBuilder result = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { result.append(newLine).append(line); } reset(); return result.toString().trim(); } private void reset() throws IOException { inputStream.reset(); } private void mark() { inputStream.mark(0); } public static void main(String[] args) throws Exception { ConvertInputStreamToStringBenchmark test = new ConvertInputStreamToStringBenchmark(); System.out.println(); System.out.println("1. apacheToInputStream : " + test.test1_apacheToInputStream().length()); System.out.println("2. guavaCharStreams : " + test.test2_guavaCharStreams().length()); System.out.println("3. jdkScanner : " + test.test3_jdkScanner().length()); System.out.println("4. jdkJava8 : " + test.test4_jdkJava8().length()); System.out.println("5. jdkJava8parallel : " + test.test5_jdkJava8parallel().length()); System.out.println("6. inputStreamReaderAndStringBuilder : " + test.test6_inputStreamReaderAndStringBuilder().length()); System.out.println("7. apacheStringWriterAndIOUtilsCopy : " + test.test7_apacheStringWriterAndIOUtilsCopy().length()); System.out.println("8. readByteArrayOutputStream : " + test.test8_readByteArrayOutputStream().length()); System.out.println("9. bufferedReaderReadLine : " + test.test9_bufferedReaderReadLine().length()); System.out.println("10. bufferedInputStreamAndByteArrayOutputStream : " + test.test10_bufferedInputStreamAndByteArrayOutputStream().length()); System.out.println("11. inputStreamReadAndStringBuilder : " + test.test11_inputStreamReadAndStringBuilder().length()); System.out.println("12. test12_bufferedReaderReadLine2 : " + test.test12_bufferedReaderReadLine2().length()); System.out.println(); Options opt = new OptionsBuilder() .include(ConvertInputStreamToStringBenchmark.class.getSimpleName()) .build(); new Runner(opt).run(); } } |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |