I was solving problems from the CSES Tree problem set recently. Typical competitive programming routine: Read problem -> Think of an approach -> Implement it -> Submit > See TLE After hours of starring at my code and trying some minor optimisations to my solution still getting back to back TLEs. I asked Claude to give C code for my approach and it was Accepted. This is not the first time i am seeing this behaviour , but today lets understand why this happened , as we cant loose to c/c++ this easily. 😅 The real culprit in my Java code was how i was taking inputs - Scanner sc = new Scanner(System.in); Turns out Scanner is amazing for writing readable code, but not amazing when you're feeding it hundreds of thousands of inputs. Why Scanner can slow things down as it does a lot behind the scenes, which is not needed here 🙂 -> Regex based tokenization -- Scanner internally uses regular expressions to identify tokens. -- Regex parsing is flexible… but obsly expensive -> Extra validation -- input format validation -- locale handling -- token boundary detection (Here we loose actually , this is good for safety , not when we need RAW SPEED) In Nutshell , scanner introduces abstraction to this process Scanner → parsing → conversions → validation → final value. Each layer adds overhead when you're calling it hundreds of thousands of times. FIX - Switch to "BufferedReader + StringTokenizer" or for Extreme performance use "Custom Fast I/O" It avoids regex and reads input in larger buffered chunks, Hence much faster input. Moral of the Story - -> If input size < 10⁴ numbers → Scanner is fine -> If input size ≥ 10⁵ or 10⁶ numbers and solving for tougher judges → avoid Scanner or avoid Java 🙂 #Java #CompetitiveProgramming #CSES #LearningInPublic
Informational as always
Interesting! Easy to focus on algorithm complexity and overlook the impact of I/O handling.
Insightful, thanks for sharing