Created
September 11, 2019 12:26
-
-
Save raszia/8a6794766a93751c949df59fdb582f9b to your computer and use it in GitHub Desktop.
Cython performance benchmark vs pure python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cybubble | |
import pybubble | |
from timeit import default_timer as timer | |
from decimal import * | |
import random | |
import copy | |
getcontext().prec = 8 | |
if __name__ == '__main__': | |
random_list = [random.randrange(0,10) for i in range(9999)] | |
random_list2 = copy.copy(random_list) | |
random_list3 = copy.copy(random_list) | |
cy_start = timer() | |
cybubble.bubble_sort(random_list2) | |
cy_end = timer() | |
py_start = timer() | |
pybubble.bubble_sort(random_list3) | |
py_end = timer() | |
print ("Python execution time:") | |
python = (Decimal(py_end) - Decimal(py_start)) | |
print (python) | |
print ("Cython execution time:") | |
cython = (Decimal(cy_end) - Decimal(cy_start)) | |
print (cython) | |
print ("Cython is "+ str(python/cython) +" x faster than pure python") | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cpdef bubble_sort(collection): | |
cdef int length | |
cdef int i | |
cdef int j | |
length = len(collection) | |
for i in range(length-1): | |
swapped = False | |
for j in range(length-1-i): | |
if collection[j] > collection[j+1]: | |
swapped = True | |
collection[j], collection[j+1] = collection[j+1], collection[j] | |
if not swapped: | |
break # Stop iteration if the collection is sorted. | |
return collection |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def bubble_sort(collection): | |
length = len(collection) | |
for i in range(length-1): | |
swapped = False | |
for j in range(length-1-i): | |
if collection[j] > collection[j+1]: | |
swapped = True | |
collection[j], collection[j+1] = collection[j+1], collection[j] | |
if not swapped: | |
break # Stop iteration if the collection is sorted. | |
return collection |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# several files with ext .pyx, that i will call by their name | |
from distutils.core import setup | |
from distutils.extension import Extension | |
from Cython.Distutils import build_ext | |
ext_modules=[ | |
Extension("cybubble", ["cybubble.pyx"]), | |
] | |
setup( | |
name = 'MyProject', | |
cmdclass = {'build_ext': build_ext}, | |
ext_modules = ext_modules, | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Command to compile cython code:
python setup.py build_ext --inplace
Command to execute the benchmark script:
python benchmark.py
Result:
Python execution time:
9.1040399
Cython execution time:
1.6505609
Cython is 5.5157249 x faster than pure python