gsoc-2014
A C++ speed-me-up trick
After a couple of months of wondering how to wrap up this blog, I decided to keep it anyway. I’ll use it for posting random tricks/tutorials whenever I get time.
I recently got my toes back into competitive programming, something I hadn’t touched for over a year, and realized how much I’d missed it. Here‘s an interesting problem I came across. It’s pretty simple from the programming perspective, needing you to know simple combinatorics (not even that, actually). Anyway, I coded it up pretty quickly and hit submit but, voila, it exceeded time constraints. I was quite sure I had the right algo, it is an O(n) solution after all. But nothing seemed to make it work, I gave up after hacking on it for a couple of days.
Today I went back to the Practice section, dug up my old code and added this to it:
ios_base::sync_with_stdio(0);
The program ran almost 4x as fast as earlier, on my own system. This is a neat trick if you’re using cin/cout (C++) and want to speed up your program. I read it in one of the very first blog posts on Codeforces. Cool, huh?