Enhancing Java Code Efficiency with BufferedReader, StringTokenizer, and long

Правка en2, от Flick__, 2024-05-22 17:02:27

Writing an Efficient Code in Codeforces is important for a problem to be Accepted. We will compare your different code runtimes by implementing them in simple standard libraries and then implementing the same code using a buffer reader, creating the necessary String Token, and changing the int variables to Long. Then, we will understand why the Buffered one is faster than the other.

Why does Scanner take longer compared to BufferedReader and DataInputStream? The primary reason is that the Scanner reads smaller bits of information from the input stream one at a time without buffering. This means that the process of reading from the input stream, which is inherently slow, must be repeated multiple times. Each read operation involves significant overhead, making the process inefficient. In contrast, BufferedReader and DataInputStream use buffering to enhance performance. Buffered input works by reading large chunks of data from the file into a buffer in one go. The program then reads from this Buffer, significantly reducing the time it needs to access the slow input stream.

This method minimizes the bottleneck caused by the input stream read operations, making the overall reading process much faster.

I have made two sample codes, one without implementing Buffer and token and the other being implemented using Buffer and token. Check the equivalent code, and then I will attach a screenshot of their runtime.

testWithoutBuffer
testWithBuffer
Теги bufferedreader, tokens, long vs int, less run time, efficiency, my first blog

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en5 Английский Flick__ 2024-05-22 18:15:41 4 Tiny change: 'iable.\n\n<spoil' -> 'iable.\n\n\n<spoil' (published)
en4 Английский Flick__ 2024-05-22 18:13:42 3172 Tiny change: 'edreader).\n[Impleme' -> 'edreader). [cut]\n[Impleme'
en3 Английский Flick__ 2024-05-22 17:03:35 14
en2 Английский Flick__ 2024-05-22 17:02:27 1918
en1 Английский Flick__ 2024-05-22 12:38:01 1638 Initial revision (saved to drafts)