Due to the resumption of the discussion about the inequality of scripted and compiled languages, I would like to offer an easy way to speed up Python, the most popular scripting language here. One of the simplest JIT compilers is Numba, a mature project included for example in Anaconda by default.
- Numba allows JIT to compile individual functions in python. This is not just another python compiler, but a regular package that can be installed using conda or pip: https://numba.pydata.org/
To make function compiled, one need to use decorator before defenition.
from numba import jit
import math
@jit(nopython=True)
def go_fast(): # JIT compiled
result = 0.0
for i in range(1000):
result += math.sqrt(i)
return result
def go_slow(): # standart cpython function
result = 0.0
for i in range(1000):
result += math.sqrt(i)
return result
%timeit go_fast() # 2.06 µs ± 3.93 ns per loop
%timeit go_slow() # 214 µs ± 1.25 µs per loop
-
Numba has some limitations, it will not be able to compile arbitrary user-defined classes, containers consisting of different types. But all common Python operations - conditions, loops, built-in collections - everything is supported out of the box. You will not need to dig into complicated type system as in cython, this is just regular Python code, possibly with minor modifications.
Supported Python features: https://numba.pydata.org/numba-doc/latest/reference/pysupported.html -
Especially it goes well with numpy, allowing you to quickly process arrays. Numpy is rich with built-in fast vectorized functions, but in real applications it is not always enough, and you have to iterate over and check something using regular Python cycles. It turns out a dramatic loss of speed, the benefits of numpy come to naught.
Benefits:
- Regular and advanced python users get performance comparable to Java / C #, get the opportunity to participate in contests/multi and write competitive code in games that require some sort of brute force. This will require some minimal adjustments, but it is much easier than rewriting the python bot in C ++ in the middle of the contest.
- These changes will not affect completely casual python users, they will write as they wrote on vanilla python.
- Perhaps for the CG team, adding a package to the existing language will be less difficult than adding a new language (PyPy for example) to the backend.