Created
September 5, 2025 01:03
-
-
Save Col-E/fb54ff9c9bdf1183aaac4b59c3fc0e53 to your computer and use it in GitHub Desktop.
1brc snippets
This file contains hidden or 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
java.vm.vendor: Eclipse Adoptium | |
java.vm.name: OpenJDK 64-Bit Server VM | |
java.vendor.version: Temurin-22+36 | |
java.vendor.url.bug: https://github.com/adoptium/adoptium-support/issues | |
os.name: Windows 10 | |
os.arch: amd64 | |
================================= | |
Samsung SSD 990 Pro 2TB (7.4 GB/s) | |
Input: 1,000,000,000 rows (13.4 GB) | |
================================= | |
Files.lines | |
Lines :: 24784 | |
FileChannel: MemorySegment | |
MemorySegment :: 2480 | |
FileChannel: MappedByteBuffer | |
MappedByteBuffer[1024] :: 85776 | |
MappedByteBuffer[4096] :: 27493 | |
MappedByteBuffer[16384] :: 14641 | |
MappedByteBuffer[81920] :: 9856 | |
MappedByteBuffer[163840] :: 9132 | |
MappedByteBuffer[1024000] :: 5907 | |
MappedByteBuffer[10240000] :: 5846 (fastest) | |
FileChannel: ByteBuffer | |
ByteBuffer[1024] :: 17874 | |
ByteBuffer[4096] :: 6430 | |
ByteBuffer[16384] :: 2739 | |
ByteBuffer[81920] :: 1576 | |
ByteBuffer[163840] :: 1460 | |
ByteBuffer[1024000] :: 1254 (fastest) | |
ByteBuffer[10240000] :: 1519 | |
BufferedInputStream(FileInputStream) | |
BufferedInputStream[1024] :: 18001 | |
BufferedInputStream[4096] :: 6514 | |
BufferedInputStream[16384] :: 2777 | |
BufferedInputStream[81920] :: 1769 | |
BufferedInputStream[163840] :: 1569 | |
BufferedInputStream[1024000] :: 1294 (fastest) | |
BufferedInputStream[10240000] :: 2964 |
This file contains hidden or 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.BufferedInputStream; | |
import java.io.FileInputStream; | |
import java.io.InputStream; | |
import java.lang.foreign.Arena; | |
import java.lang.foreign.MemorySegment; | |
import java.lang.foreign.ValueLayout; | |
import java.nio.ByteBuffer; | |
import java.nio.MappedByteBuffer; | |
import java.nio.channels.FileChannel; | |
import java.nio.charset.StandardCharsets; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
import java.nio.file.Paths; | |
import java.nio.file.StandardOpenOption; | |
import java.util.stream.Stream; | |
public class ReadSpeedTest { | |
static final int[] bufferSizes = new int[]{1024, 4096, 16_384, 81_920, 163_840, 1_024_000, 10_240_000}; | |
public static void main(String[] args) throws Throwable { | |
// System.getProperties().forEach((k, v) -> { | |
// System.out.println(k + ": " + v); | |
// }); | |
// System.out.println("================================="); | |
// System.out.println("Samsung SSD 990 Pro 2TB"); | |
// System.out.println("Ryzen 9 7950X 16 core (5.4 GHz average while running this)"); | |
// System.out.println("Input: 1,000,000,000 rows (13.4 GB)"); | |
// System.out.println("================================="); | |
Path target = Paths.get("measurements.txt"); | |
lines(target); | |
fileChannel_MemorySegment_Arena(target); | |
fileChannel_MemorySegment_MappedByteBuffer(target); | |
fileChannel_ByteBuffer(target); | |
bufferedFileInputStream(target); | |
} | |
private static void lines(Path target) { | |
// Backed by FileChannel + custom spliterator | |
System.out.println("\nFiles.lines"); | |
long start = System.currentTimeMillis(); | |
try (Stream<String> lines = Files.lines(target, StandardCharsets.ISO_8859_1)) { | |
// StringBuilder content = new StringBuilder(); | |
lines.forEach(line -> { | |
// content.append(line); | |
}); | |
} catch (Throwable t) { | |
throw new RuntimeException(t); | |
} | |
long end = System.currentTimeMillis(); | |
System.out.println("Lines :: " + (end - start)); | |
} | |
private static void bufferedFileInputStream(Path target) { | |
System.out.println("\nBufferedInputStream(FileInputStream)"); | |
for (int bufferSize : bufferSizes) { | |
long start = System.currentTimeMillis(); | |
byte[] buffer = new byte[bufferSize]; | |
try (InputStream is = new BufferedInputStream(new FileInputStream(target.toFile()), bufferSize)) { | |
int read; | |
// StringBuilder content = new StringBuilder(); | |
while ((read = is.read(buffer)) != -1) { | |
// for (int i = 0; i < read; i++) { | |
// content.append((char) buffer[i]); // Assuming ASCII text content (1 byte --> 1 char) | |
// } | |
} | |
} catch (Throwable t) { | |
throw new RuntimeException(t); | |
} | |
long end = System.currentTimeMillis(); | |
System.out.println("BufferedInputStream[" + bufferSize + "] :: " + (end - start)); | |
} | |
} | |
private static void fileChannel_ByteBuffer(Path target) { | |
System.out.println("\nFileChannel: ByteBuffer"); | |
for (int bufferSize : bufferSizes) { | |
long start = System.currentTimeMillis(); | |
try (FileChannel channel = FileChannel.open(target, StandardOpenOption.READ)) { | |
ByteBuffer buffer = ByteBuffer.allocate(bufferSize); // Allocate a buffer for reading | |
int bytesRead; | |
// StringBuilder content = new StringBuilder(); | |
while ((bytesRead = channel.read(buffer)) != -1) { | |
buffer.flip(); | |
// while (buffer.hasRemaining()) { | |
// content.append((char) buffer.get()); | |
// } | |
buffer.clear(); | |
} | |
} catch (Throwable t) { | |
throw new RuntimeException(t); | |
} | |
long end = System.currentTimeMillis(); | |
System.out.println("ByteBuffer[" + bufferSize + "] :: " + (end - start)); | |
} | |
} | |
private static void fileChannel_MemorySegment_MappedByteBuffer(Path target) { | |
System.out.println("\nFileChannel: MappedByteBuffer"); | |
for (int bufferSize : bufferSizes) { | |
long start = System.currentTimeMillis(); | |
try (FileChannel channel = FileChannel.open(target, StandardOpenOption.READ)) { | |
long size = channel.size(); | |
long position = 0; | |
while (position < size) { | |
// StringBuilder sb = new StringBuilder(); | |
int segmentSize = (size - position) > bufferSize ? bufferSize : (int) (size - position); | |
MappedByteBuffer segment = channel.map(FileChannel.MapMode.READ_ONLY, position, segmentSize); | |
for (long l = 0; l < segmentSize; l++) { | |
char c = (char) segment.get(); | |
// sb.append(c); | |
} | |
position += segmentSize; | |
} | |
} catch (Throwable t) { | |
throw new RuntimeException(t); | |
} | |
long end = System.currentTimeMillis(); | |
System.out.println("MappedByteBuffer[" + bufferSize + "] :: " + (end - start)); | |
} | |
} | |
private static void fileChannel_MemorySegment_Arena(Path target) { | |
System.out.println("\nFileChannel: MemorySegment"); | |
long start = System.currentTimeMillis(); | |
try (FileChannel channel = FileChannel.open(target, StandardOpenOption.READ)) { | |
long size = channel.size(); | |
MemorySegment segment = channel.map(FileChannel.MapMode.READ_ONLY, 0L, size, Arena.ofAuto()); | |
// StringBuilder sb = new StringBuilder(); | |
for (long l = 0; l < size; l++) { | |
char c = (char) segment.get(ValueLayout.JAVA_BYTE, l); | |
// sb.append(c); | |
} | |
} catch (Throwable t) { | |
throw new RuntimeException(t); | |
} | |
long end = System.currentTimeMillis(); | |
System.out.println("MemorySegment :: " + (end - start)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment