Can we ditch Python for Competitive Coding?

We are going to know why should we ditch python for competitive coding but we have heard that-

Written By: Prateek Bajpai

“Python is the language of this generation”
“If you don’t learn Python, you’ll not succeed in this field”
“Python is so easy to work on, it doesn’t even feel like you’re coding”

These are some statements I’ve heard over the past few years by peers, teachers, online forums, etc. And they’re true! I mean I don’t disagree with these statements even a bit, Python indeed is a highly sought after skill in the industry these days. It’s easy to learn, dynamically typed, object oriented, compact, has thousands of libraries, can handle big integers without any modifications, and many other things. It is the language of choice for fields like Machine Learning, Social Network analysis, Data Analysis, etc. But there is one more thing about Python – It’s Dead Slow. Read ahead to know the reason to ditch python for competitive coding.

And it’s so slow that most platforms for competitive coding add a 5x multiplier to their time limits if the problem is submitted in Python. This means that if the time limit they’ve set for C/C++ submissions if 1.2 secs, the time limit for Python would be 6 secs. Don’t believe me?
Here is a screenshot from the Codechef blog:

It is available here also.

There are many reasons why Python is so slow, and I’ll not go into much depths of the problems. I’ll just give you some overview though.

1. It is an interpreted language

It is fact every which every CS student will get in their freshman year that interpreters are slower than compilers. C/C++ are compiled languages, and thus the difference in speed shows.

2. It is a dynamically typed language

The variables’ types are identified at runtime, and hence, all the memory allocation, type conversion, and all other tasks which languages like C and C++ do at compile time, are done at runtime in python, hence slowing down the running time.

3. It doesn’t use the JIT compiler

JIT or Just-in-Time compiler is a kind of compiler which translates the source code, or platform-independent bytecode directly into native machine code at run-time, and so JIT compilation is also known as Dynamic compilation. JIT compilation is faster mainly because it has access to the dynamic runtime information, enabling it to better its optimizations for the machine it is running on. Machine codes generated by JIT compiler are more optimized for the machine it is running on. The working of any compiler is complex, and so it cannot be discussed much in depth in a single paragraph. If you’re interested to know how JIT compilation works in detail, I suggest that you surf the web for some amazing articles by IBM or Eclipse, or simple search stackoverflow! Some languages that use JIT compiler are Java and C#.

There is also a compiled version of Python known as PyPy, which uses the JIT compiler. It’s somewhat better than Python in terms of running time, but it’s just like climbing two steps of an infinite ladder, nothing else. Chances are that you’ll get a TLE (Time Limit Exceeded) error in competitive coding platforms in some complex problems, even if you’re using a highly optimized algorithm on PyPy.

C and C++ codes have longer development times, but you can perfect that with practice, and it won’t hamper much of your time in a competition. But you surely will gain some minus penalty points if you write a short code in Python or PyPy and end up getting TLE even if your algorithm was perfect. There must be some reason that all top submissions in any coding contest have C++ as the coding language!

Below are the screenshots of some of my submission results on Hackerrank and Codechef, note that the failed submissions have TLE as the reason of failure and are submitted in either Python of PyPy.

Just notice the huge difference in running time of C(left) and PyPy(right)-

ditch python in competitive coding

A classical problem on divide and conquer approach, innocently throwing a TLE at my face using Python.

Insertion Sort advanced analysis.

Note that in both of these cases, I just copied the code from my Python submission, pasted it into the C window, made the necessary syntactical changes, and there it was, C language showing its magic!
There is one amazing website, known as the Computer Language Benchmarks Game, which has done this amazing compilation of time taken by various programming languages to run a particular kind on algorithm in different settings. The screenshot below shows the running time comparison of C vs Python for some kind of algorithms:

C vs Python

Look at the huge difference between running times of C and Python, where Python is found to be approx. 8.3 times slower than C!

These reasons are enough for you to get yourself attached to C or C++ for competitive programming. It’s worth the effort!

Read other articles written by Prateek Bajpai here.
Read other articles on Competitive Coding.

Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *