(Feb-14-2021, 01:32 AM)Skaperen Wrote: i am curious how easy it might be go a step further a generate C or machine codes to increase runtime execution performance in certain cases.
It's not easy
PyPy has been working on this for 14 years.
PyPy generate C code as just one step to the Python code faster.
David Beazley explain this
here
PyPy easy to use just run
pypy3
instead of
Python
.
Usually is 1-2 version behind Python,so PyPy is on version 3.7 now.
# loop_test.py
import time
start_time = time.time()
total = 0
for i in range(1, 10000):
for j in range(1, 10000):
total += i + j
print(f"The result is {total}")
end_time = time.time()
print(f"It took {end_time-start_time:.2f} seconds to compute")
Output:
# Using Python 3.9.1
G:\pypy3.7
λ python loop_test.py
The result is 999800010000
It took 16.97 seconds to compute
# Using PyPy 3.7
G:\pypy3.7
λ pypy3 loop_test.py
The result is 999800010000
It took 2.72 seconds to compute
Linux take down with
pyenv and use.
# Using Python
tom@tom-VirtualBox:~$ python -V
Python 3.9.1
tom@tom-VirtualBox:~$ python loop_test.py
The result is 999800010000
It took 23.34 seconds to compute
# Take down PyPy
tom@tom-VirtualBox:~$ pyenv install pypy3.7-7.3.3
continue with installation? (y/N) y
Downloading pypy3.7-v7.3.3-linux64.tar.bz2...
-> https://downloads.python.org/pypy/pypy3.7-v7.3.3-linux64.tar.bz2
Installing pypy3.7-v7.3.3-linux64...
# Set system wide
tom@tom-VirtualBox:~$ pyenv global pypy3.7-7.3.3
tom@tom-VirtualBox:~$ pypy3 -V
Python 3.7.9 (7e6e2bb30ac5, Nov 18 2020, 10:55:52)
[PyPy 7.3.3-beta0 with GCC 7.3.1 20180303 (Red Hat 7.3.1-5)]
# Run
tom@tom-VirtualBox:~$ pypy3 loop_test.py
The result is 999800010000
It took 1.31 seconds to compute
There is lot going on in this field as eg
Cython,
Numba.
Python is big in Data Science where now GPU is prefeed over CPU to speed thing up.
Data Science on GPU