Python is an amazingly user-friendly language with the only flaw of being slow. In comparison to C, C++ and Java, it is quite slower. On Online coding platforms, if C/C++ limit provided is X. Usually, in Java time provided is 2X and Python, it’s 5X.
To improve the speed of code execution for input/output intensive problems, languages have various input and output procedures.
An Example Problem :
Consider a question of finding the sum of N numbers inputted from the user.
Input a number N.
Input N numbers separated by a single space in a line.
Input : 5 1 2 3 4 5 Output : 15
Different Python solutions for above Problem :
Normal Method Python: (Python 2.7)
1. raw_input() takes an optional prompt argument. It also strips the trailing newline character from the string it returns.
2. print is just a thin wrapper that formats the inputs (space between args and newline at the end) and calls the write function of a given object.
A bit faster method using inbuilt stdin, stdout: (Python 2.7)
1. sys.stdin on the other hand is a File Object. It is like creating any other file object one could create to read input from the file. In this case, the file will be standard input buffer.
2. stdout.write(‘D\n’) is faster than print ‘D’.
3. Even faster is to write all once by stdout.write(“”.join(list-comprehension)) but this makes memory usage dependent on size of input.
Difference in time:
Timing summary (100k lines each)
Print : 6.040 s
Write to file : 0.122 s
Print with Stdout : 0.121 s
Adding a buffered pipe io: (Python 2.7)
1. Simply, adding the buffered IO code before your submission code to make the output faster.
2. The benefit of io.BytesIO objects is that they implement a common-ish interface (commonly known as a ‘file-like’ object). BytesIO objects have an internal pointer and for every call to read(n) the pointer advances.
3. The atexit module provides a simple interface to register functions to be called when a program closes down normally. The sys module also provides a hook, sys.exitfunc, but only one function can be registered there. The atexit registry can be used by multiple modules and libraries simultaneously.
While handling a large amount of data usually, the normal method fails to execute within the time limit. Method 2 helps in maintaining a large amount of I/O data. Method 3 is the fastest. Usually, handling of input data files greater than 2 or 3 MBs is helped via method 2 and 3.
Note : above mention codes are in Python 2.7, to use in Python 3.X versions. Simply replace the raw_input() with Python 3.X’s input() syntax. Rest should work fine.
This article is contributed by Shubham Saxena. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Input/Output from external file in C/C++, Java and Python for Competitive Programming
- Input/Output from external file in C/C++, Java and Python for Competitive Programming | Set 2
- C++: Methods of code shortening in competitive programming
- getchar_unlocked() - faster input in C/C++ for Competitive Programming
- Python in Competitive Programming
- Tips and Tricks for Competitive Programmers | Set 2 (Language to be used for Competitive Programming)
- How can competitive programming help you get a job?
- C++ tricks for competitive programming (for C++ 11)
- How to become a master in competitive programming?
- Bit Tricks for Competitive Programming
- Fast I/O for Competitive Programming
- A Better Way To Approach Competitive Programming
- How to begin with Competitive Programming?
- Competitive Programming: Conquering a given problem
- Fast I/O in Java in Competitive Programming
Improved By : HARSHGUPTA5