Hello everyone.
Context
After coding in Java since I started CP, I thought of switching to Rust.
After using templates for fast IO, I thought of creating a decently working scanner myself.
I tried implementing input in 2 ways, but the way that I thought would be faster is slower, and I couldn't figure out why that is the case.
TLDR
Implementation 1: Taking input line-by-line.
Implementation 2: Storing bytes in a static buffer array.
I thought that the 2nd method is faster, since there are a lot of lines ($$$10^6$$$ test cases), and the buffer can store multiple lines in it.
Can someone please explain this?









I maybe wrong but are u sure that the static buffer array was slower in runtime, not due to compile-time.I didn't saw the code but afaik you might be declaring a large static buffer array which may increase the compile-time significantly
wrong branch
I believe the allocation is done at runtime, and the compilation time is not included in the time shown.
I tried different buffer sizes, the one with $$$2^{20}$$$ took more time, but the $$$2^{16}$$$ one took less time than smaller buffer sizes. You can check the other submissions.
I cannot yet understand why first is faster, but note that you use BufReader in both cases, so no need to bufferization on top of it
Makes sense. I tried using the internal buffer instead of creating a new one (submission).
It is faster than the previous code, but still slower than the first idea.
My guess is the number of Strings (one for each input).
Edit: The default capacity of the strings was 16, which is not enough for 64-bit integers. I increased it to 20 and got close to 200ms (Submission)
Maybe I can use a single string for primitive types (instead of creating them for each input) and that'll reduce the time.
Edit 2: I guess this will work fine for now. Maybe in future I'll dig deeper into making the IO faster than this. Thank you for your insight.
Could be due to increased CPU cache utilization for input buffers, which leaves less room for other data needed during runtime. Anyway, best way to figure out why is to profile it.
Is there in Rust something that reads the entire file (using WinAPI or unix system calls), like this in C++?
Wouldn't that consume a lot of memory?