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?

 
2
Kudos
 
2
Kudos

Now read this

31 May ‘14

Managed to link my Report Bug button to a new page. Created a PostgreSQL database and did a syncdb with website folder Investigated django directory structure. Thinking of the possibility to make this into a new app, like website,... Continue →