From 8ffc36ed039245cac0a2a8c0f578c838c4bcdd11 Mon Sep 17 00:00:00 2001 From: Aman Ahmed Siddiqui Date: Fri, 28 Oct 2022 10:50:03 +0530 Subject: [PATCH 1/2] Added Cython Folder --- Cython/.gitignore | 4 ++++ Cython/README.md | 7 +++++++ 2 files changed, 11 insertions(+) create mode 100644 Cython/.gitignore create mode 100644 Cython/README.md diff --git a/Cython/.gitignore b/Cython/.gitignore new file mode 100644 index 0000000..04b8a92 --- /dev/null +++ b/Cython/.gitignore @@ -0,0 +1,4 @@ +*.c +*.so + +venv/ \ No newline at end of file diff --git a/Cython/README.md b/Cython/README.md new file mode 100644 index 0000000..8378986 --- /dev/null +++ b/Cython/README.md @@ -0,0 +1,7 @@ +# Contribution Guidelines for this folder + +Create new issues if not available and fill this folder with code snippets written in Python. No spamming is allowed, the code must be genuinely solving a problem or might contain usefuls insights for this language. + +- Create a new folder with your code snippet and add a README.md to this new folder. +- Add possible explanations along with screenshots and inner working of this snippets +- The snippet should be more than 50 lines of code. From 5f12c6b6952751b51a411c9ff8c05f994dacb188 Mon Sep 17 00:00:00 2001 From: Aman Ahmed Siddiqui Date: Fri, 28 Oct 2022 10:54:56 +0530 Subject: [PATCH 2/2] Created Bubble Sort Using Cython --- Cython/Bubble Sort/cython_sorting/__init__.py | 0 .../cython_sorting/cython_bubble_sort.pyx | 23 +++++++++++ Cython/Bubble Sort/main.py | 38 +++++++++++++++++++ Cython/Bubble Sort/python_sorting/__init__.py | 0 .../python_sorting/python_bubble_sort.py | 10 +++++ 5 files changed, 71 insertions(+) create mode 100644 Cython/Bubble Sort/cython_sorting/__init__.py create mode 100644 Cython/Bubble Sort/cython_sorting/cython_bubble_sort.pyx create mode 100644 Cython/Bubble Sort/main.py create mode 100644 Cython/Bubble Sort/python_sorting/__init__.py create mode 100644 Cython/Bubble Sort/python_sorting/python_bubble_sort.py diff --git a/Cython/Bubble Sort/cython_sorting/__init__.py b/Cython/Bubble Sort/cython_sorting/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/Cython/Bubble Sort/cython_sorting/cython_bubble_sort.pyx b/Cython/Bubble Sort/cython_sorting/cython_bubble_sort.pyx new file mode 100644 index 0000000..d62e262 --- /dev/null +++ b/Cython/Bubble Sort/cython_sorting/cython_bubble_sort.pyx @@ -0,0 +1,23 @@ +# cython: language_level=3 + +from array import array +from cpython cimport array + + +cpdef int[:] c_sort(int[:] arr, int n): + cdef int i, j + + for i in range(n): + for j in range(n - i - 1): + if arr[j] > arr[j+1]: + arr[j], arr[j+1] = arr[j+1], arr[j] + + return arr + + +cpdef list sort(list arr): + cdef int[:] c_arr = array("i", arr) + cdef int[:] s_arr + + s_arr = c_sort(c_arr, len(arr)) + return list(s_arr) \ No newline at end of file diff --git a/Cython/Bubble Sort/main.py b/Cython/Bubble Sort/main.py new file mode 100644 index 0000000..0e2ac48 --- /dev/null +++ b/Cython/Bubble Sort/main.py @@ -0,0 +1,38 @@ +import time +import random + + +def timeit(func= None, *args, **kwargs): + a = time.time() + returned_items = func(*args, **kwargs) + b = time.time() + return (returned_items, b - a) + +generate_values = lambda n : [random.randint(-n, n) for i in range(n)] + +# __________________________________________________________ # +#### ________ Change Only From Below Section ________ #### + +def main(): + from cython_sorting import cython_bubble_sort + from python_sorting import python_bubble_sort + + values = generate_values(n= 10000) + array1, t1 = timeit( + cython_bubble_sort.sort, + values.copy() + ) + array2, t2 = timeit( + python_bubble_sort.sort, + values.copy() + ) + + print(f"Cython takes: {t1:0.3f} seconds") + print(f"Python takes: {t2:0.3f} seconds") + print(f"Cython is {t2/t1:0.2f}x faster than Python!") + print(f"Same returned value: {array1 == array2}") + # print(array2) + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/Cython/Bubble Sort/python_sorting/__init__.py b/Cython/Bubble Sort/python_sorting/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/Cython/Bubble Sort/python_sorting/python_bubble_sort.py b/Cython/Bubble Sort/python_sorting/python_bubble_sort.py new file mode 100644 index 0000000..53cb466 --- /dev/null +++ b/Cython/Bubble Sort/python_sorting/python_bubble_sort.py @@ -0,0 +1,10 @@ + +def sort(arr): + n = len(arr) + + for i in range(n): + for j in range(n - i - 1): + if(arr[j] > arr[j+1]): + arr[j], arr[j+1] = arr[j+1], arr[j] + + return arr \ No newline at end of file