/* * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package oracle.jmh; import org.openjdk.jmh.annotations.Benchmark; import org.openjdk.jmh.annotations.BenchmarkMode; import org.openjdk.jmh.annotations.Mode; import org.openjdk.jmh.annotations.OutputTimeUnit; import org.openjdk.jmh.annotations.Param; import org.openjdk.jmh.annotations.Scope; import org.openjdk.jmh.annotations.Setup; import org.openjdk.jmh.annotations.State; import java.io.BufferedWriter; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.concurrent.TimeUnit; import java.util.function.IntFunction; import java.util.stream.Stream; import static java.util.stream.Collectors.joining; @State(Scope.Thread) @BenchmarkMode(Mode.AverageTime) @OutputTimeUnit(TimeUnit.MICROSECONDS) public class LinesTest { public enum LineSeparator { N("\n"), R("\r"), RN("\r\n"); public final String value; LineSeparator(String value) { this.value = value; } } static Path generate(IntFunction f, int n, LineSeparator ls, boolean endLineSep) throws IOException { Path lines = Files.createTempFile("lines", null); BufferedWriter bw = Files.newBufferedWriter(lines); for (int i = 0; i < n - 1; i++) { bw.write(f.apply(i)); bw.write(ls.value); } bw.write(f.apply(n - 1)); if (endLineSep) bw.write(ls.value); bw.flush(); bw.close(); lines.toFile().deleteOnExit(); return lines; } @Param("100000") int LINES; @Param("80") int COLUMNS; @Param("N") LineSeparator SEP; Path p; @Setup public void setUp() throws IOException { String line = Stream.generate(() -> "A").limit(COLUMNS).collect(joining()); p = generate(i -> line, LINES, SEP, true); } @Benchmark public long stream() throws IOException { return Files.lines(p).count(); } @Benchmark public long streamParallel() throws IOException { return Files.lines(p).parallel().count(); } @Benchmark public long bufferedReader() throws IOException { return Files.newBufferedReader(p).lines().count(); } @Benchmark public long bufferedReaderParallel() throws IOException { return Files.newBufferedReader(p).lines().parallel().count(); } public static void main(String[] args) throws IOException { String line = Stream.generate(() -> "A").limit(80).collect(joining()); Path p = generate(i -> line, 1000000, LineSeparator.N, true); System.in.read(); long c = 0; while (true) { // c += Files.newBufferedReader(p).lines().count(); c += Files.lines(p).parallel().count(); System.out.println(c); } } }