Last active
August 31, 2018 09:13
-
-
Save Crydust/ec6daf292f2adb1b96ec0b3d8b06af8e to your computer and use it in GitHub Desktop.
Java versus Go: a performance comparison (see https://www.bernhardwenzel.com/articles/java-vs-go-performance/ )
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.PrintWriter; | |
import java.nio.charset.StandardCharsets; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
import java.nio.file.Paths; | |
import java.time.Duration; | |
import java.time.Instant; | |
import java.time.ZonedDateTime; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.concurrent.ArrayBlockingQueue; | |
import java.util.concurrent.BlockingQueue; | |
import java.util.concurrent.ExecutorService; | |
import java.util.concurrent.Executors; | |
import java.util.regex.Pattern; | |
class Scratch { | |
public static final Pattern pattern = Pattern.compile("^[^#]*#[15][15]1110+$"); | |
public static void main(String[] args) throws IOException, InterruptedException { | |
Thread.sleep(10_000); | |
// createSampleLogfile(); | |
loopingWithCounter(); // PT3.307225107S | |
sequential(); // PT13.547034899S | |
parallel(); // PT4.961340736S | |
Parallel.main(new String[0]); // PT5.876S | |
} | |
private static void createSampleLogfile() throws IOException { | |
// Creating the sample log file | |
final Path logFile = Paths.get("c:\\temp\\log-sample.txt"); | |
final int numberOfLines = 20_000_000; | |
System.out.println("Creating log file: " + logFile); | |
final ZonedDateTime now = ZonedDateTime.now(); | |
try (PrintWriter writer = new PrintWriter(Files.newBufferedWriter(logFile, StandardCharsets.UTF_8))) { | |
for (int i = 0; i < numberOfLines; i++) { | |
final ZonedDateTime date = now.plusSeconds(i); | |
writer.printf("%s -- log entry #%d%n", date, i); | |
if (i % 100000 == 0) { | |
System.out.printf("... created %d lines%n", i); | |
} | |
} | |
} | |
System.out.printf("Done writing %s lines%n", logFile); | |
} | |
private static void loopingWithCounter() throws IOException { | |
//Looping with counter | |
final long start = System.nanoTime(); | |
System.out.println("Started: " + ZonedDateTime.now()); | |
final Path logFile = Paths.get("c:\\temp\\log-sample.txt"); | |
final long i = Files.lines(logFile) | |
.count(); | |
System.out.println("i = " + i); | |
final long end = System.nanoTime(); | |
System.out.println(Duration.ofNanos(end - start)); | |
} | |
private static void sequential() throws IOException { | |
//Sequential | |
final long start = System.nanoTime(); | |
System.out.println("Started: " + ZonedDateTime.now()); | |
final Path logFile = Paths.get("c:\\temp\\log-sample.txt"); | |
Files.lines(logFile) | |
.filter(pattern.asPredicate()) | |
.forEach(line -> System.out.println("Match: " + line)); | |
final long end = System.nanoTime(); | |
System.out.println(Duration.ofNanos(end - start)); | |
} | |
private static void parallel() throws IOException { | |
//Parallel | |
final long start = System.nanoTime(); | |
System.out.println("Started: " + ZonedDateTime.now()); | |
final Path logFile = Paths.get("c:\\temp\\log-sample.txt"); | |
Files.lines(logFile) | |
.parallel() | |
.filter(pattern.asPredicate()) | |
.forEach(line -> System.out.println("Match: " + line)); | |
final long end = System.nanoTime(); | |
System.out.println(Duration.ofNanos(end - start)); | |
} | |
} | |
class Parallel { | |
private static final Pattern pattern = Pattern.compile("^[^#]*#[15][15]1110+$"); | |
private static final int numberOfWorkers = 2; | |
private static final BlockingQueue<String> queue = new ArrayBlockingQueue<>(512); | |
private static final ExecutorService executor = Executors.newFixedThreadPool(numberOfWorkers, r -> { | |
final Thread thread = new Thread(r); | |
thread.setDaemon(true); | |
return thread; | |
}); | |
public static void main(String[] args) throws IOException, InterruptedException { | |
Instant start = Instant.now(); | |
System.out.println("Started: " + start); | |
final Path logFile = Paths.get("c:\\temp\\log-sample.txt"); | |
// start workers | |
for (int i = 0; i < numberOfWorkers; i++) { | |
Worker worker = new Worker(queue, i); | |
executor.submit(worker); | |
} | |
executor.shutdown(); | |
try (BufferedReader reader = Files.newBufferedReader(logFile, StandardCharsets.UTF_8)) { | |
String line; | |
while ((line = reader.readLine()) != null) { | |
queue.put(line); | |
} | |
} | |
while (!queue.isEmpty()) { | |
System.out.println("Waiting ..."); | |
} | |
Instant end = Instant.now(); | |
System.out.println(Duration.between(start, end)); | |
} | |
static class Worker implements Runnable { | |
private final BlockingQueue<String> queue; | |
private final int id; | |
public Worker(BlockingQueue<String> queue, int id) { | |
this.queue = queue; | |
this.id = id; | |
} | |
public void run() { | |
try { | |
final List<String> lines = new ArrayList<>(); | |
final int maxelements = 48; | |
while (true) { | |
lines.clear(); | |
if (queue.drainTo(lines, maxelements) == 0) { | |
lines.add(queue.take()); | |
queue.drainTo(lines, maxelements - 1); | |
} | |
for (final String line : lines) { | |
if (pattern.matcher(line).matches()) { | |
System.out.print(String.format("[%s] Match: %s\n", id, line)); | |
} | |
} | |
} | |
} catch (InterruptedException ex) { | |
Thread.currentThread().interrupt(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment