How To Make Your I/O Faster In A Coding Competition?

How to make your I/O faster in a Coding Competition?
How to make your I/O faster in a Coding Competition?

The world of competitive programming depends upon your problem-solving skills and also how fast you can solve them? When we are talking about getting the first position or ranking in the competition then even a matter of second can be the reason for scoring low.

Even let’s just talk about regular contests the deciding factor when you have solved problems is the time, like if you have solved three questions, many of the other participants also have solved three then the deciding factor is the time.

Introduction

If you are someone participating in regular contests then this article will guide you to minimise your time in writing the I/O operations and help you directly jump into the problem-solving part. In any contest. It is very important to use the fast I/O operations to make things faster for you and can help you achieve better ranks. Moreover, if you are using CPP for your problem solving then I will jot down some tips to improve the same.

Important Tricks For Fast I/O:

  • Use of scanf/printf instead of cout/cin. Making use of scanf/printf makes input/output much faster than normal.
  • If we direly want to use cin/cout then we can include a statement ios_base::sync_with_stdio(false); which is by default true and it synchronises the cpp and c input output streams, which is made false by this statement and hence makes it faster.
    • cin with operator>> : 540ms
    • cin with operator>> (with sync_with_stdio(false)): 100ms
    • scanf (two ints at a time): 330ms
  • Use of #include <bits/stdc++.h> which is a parent header file of all the STL classes and hence avoids the use of including all the needed header files one by one. This saves a lot of time from typing away the individual header files.
  • You can also use “n” for new-line rather than an endl which enables a flushing stream and takes more time although which is not necessary for competitive programming.
  • To use integers as input we can also use fast scan for the same purpose. Fastscan make the integer input much faster.
  • We can use getchar_unlocked() instead of getchar to make faster input in both c and c++ as the former is not thread-safe and hence avoids all the mutual exclusion principles and hence are quite fast in the application.
    • Handwritten int parser that uses getchar() to directly read the input: 170ms
    • Reading entire input to char array with fread and using my own int parser: 30ms
  • We can use scanf instead of getchar as scanf is much faster than getchar.

Frequently Asked Questions

Why is it necessary to include fast I/O?

The ranking in any contest is also done on the basis of time after taking number of problems solved as draw. Hence it becomes quite essential to break this tie using time parameter. Hence, it is always advisory to take faster Input/output.

Why we do not use fast I/O everywhere?

Fast I/O comes with its own disadvantages, most of them are not safe for any application-level programming. It causes memory leaks and also can have security issues.

What are some of the tricks for faster I/O in google code jam, ICPC?

One of the very uses of such trick is fast scan which you can use in such competitions for better results.

Conclusion

Hence, we can see that how important is fast I/O, not only it boosts your execution speed but also having such a template handy helps you directly jump into the problem rather than thinking about the input-output operations. It is always advisory to use fast I/O methods in any coding contests for better results.

Hope this article is can help an aspiring developer and competitive coder to achieve better ranks and improve the ratings.

If you need help in the coding examination, please refer to our course page and you can also practice competition problems online.

By Aniruddha Guin