diff --git a/Numpy using Cython Source code/BoundCheck_Indices_Source code/array5.pyx b/Numpy using Cython Source code/BoundCheck_Indices_Source code/array5.pyx new file mode 100644 index 0000000..ff0ce4a --- /dev/null +++ b/Numpy using Cython Source code/BoundCheck_Indices_Source code/array5.pyx @@ -0,0 +1,4 @@ +import array5_boundCheck +import numpy +arr = numpy.arange(1000000000, dtype=numpy.int) +array5_boundCheck.do_calc(arr) \ No newline at end of file diff --git a/Numpy using Cython Source code/BoundCheck_Indices_Source code/array5_boundCheck.pyx b/Numpy using Cython Source code/BoundCheck_Indices_Source code/array5_boundCheck.pyx new file mode 100644 index 0000000..89d819b --- /dev/null +++ b/Numpy using Cython Source code/BoundCheck_Indices_Source code/array5_boundCheck.pyx @@ -0,0 +1,26 @@ +import time +import numpy +cimport numpy +cimport cython + +ctypedef numpy.int_t DTYPE_t + +@cython.boundscheck(False) # turn of bound cehking for entire function +@cython.wraparound(False) # turn off negative index wrapping for entire function + +def do_calc(numpy.ndarray[DTYPE_t, ndim=1] arr): + cdef int maxval + cdef unsigned long long int total + cdef int k + cdef double t1, t2, t + cdef int arr_shape = arr.shape[0] + + t1 = time.time() + + for k in range(arr_shape): + total = total + arr[k] + print "Total = ", total + + t2 = time.time() + t = t2 - t1 + print("%.10f" % t) \ No newline at end of file diff --git a/Numpy using Cython Source code/BoundCheck_Indices_Source code/setup.py b/Numpy using Cython Source code/BoundCheck_Indices_Source code/setup.py new file mode 100644 index 0000000..181862f --- /dev/null +++ b/Numpy using Cython Source code/BoundCheck_Indices_Source code/setup.py @@ -0,0 +1,8 @@ +from distutils.core import setup, Extension +from Cython.Build import cythonize +import numpy + +setup( + ext_modules = cythonize("array5.pyx"), + include_dirs=[numpy.get_include()] + ) diff --git a/Numpy using Cython Source code/Indexing Source code/array4.pyx b/Numpy using Cython Source code/Indexing Source code/array4.pyx new file mode 100644 index 0000000..ae5a10e --- /dev/null +++ b/Numpy using Cython Source code/Indexing Source code/array4.pyx @@ -0,0 +1,4 @@ +import array4_indexing +import numpy +arr = numpy.arange(1000000000, dtype=numpy.int) +array4_indexing.do_calc(arr) \ No newline at end of file diff --git a/Numpy using Cython Source code/Indexing Source code/array4_indexing.pyx b/Numpy using Cython Source code/Indexing Source code/array4_indexing.pyx new file mode 100644 index 0000000..b314d90 --- /dev/null +++ b/Numpy using Cython Source code/Indexing Source code/array4_indexing.pyx @@ -0,0 +1,20 @@ +import time +import numpy +cimport numpy + +ctypedef numpy.int_t DTYPE_t + +def do_calc(numpy.ndarray[DTYPE_t, ndim=1] arr): + cdef int maxval + cdef unsigned long long int total + cdef int k + cdef double t1, t2, t + cdef int arr_shape = arr.shape[0] + + t1=time.time() + for k in range(arr_shape): + total = total + arr[k] + print "Total =", total + t2=time.time() + t = t2-t1 + print("%.20f" % t) \ No newline at end of file diff --git a/Numpy using Cython Source code/Indexing Source code/setup.py b/Numpy using Cython Source code/Indexing Source code/setup.py new file mode 100644 index 0000000..f8aa2f0 --- /dev/null +++ b/Numpy using Cython Source code/Indexing Source code/setup.py @@ -0,0 +1,8 @@ +from distutils.core import setup, Extension +from Cython.Build import cythonize +import numpy + +setup( + ext_modules = cythonize("array4.pyx"), + include_dirs=[numpy.get_include()] + ) diff --git a/Numpy using Cython Source code/NumPy with cython Source code/numAddCDType.pyx b/Numpy using Cython Source code/NumPy with cython Source code/numAddCDType.pyx new file mode 100644 index 0000000..bbf29de --- /dev/null +++ b/Numpy using Cython Source code/NumPy with cython Source code/numAddCDType.pyx @@ -0,0 +1,22 @@ +import time +import numpy +cimport numpy + +cdef unsigned long long int maxval +cdef unsigned long long int total +cdef int k +cdef double t1, t2, t +cdef numpy.ndarray arr + +maxval = 1000000000 +arr = numpy.arange(maxval) + +t1 = time.time() + +for k in arr: + total = total + k +print "Total =", total + +t2 = time.time() +t = t2 - t1 +print("%.20f" % t) diff --git a/Numpy using Cython Source code/NumPy with cython Source code/setup.py b/Numpy using Cython Source code/NumPy with cython Source code/setup.py new file mode 100644 index 0000000..c6cb141 --- /dev/null +++ b/Numpy using Cython Source code/NumPy with cython Source code/setup.py @@ -0,0 +1,8 @@ +from distutils.core import setup, Extension +from Cython.Build import cythonize +import numpy + +setup( + ext_modules = cythonize("numAddCDType.pyx"), + include_dirs=[numpy.get_include()] + ) diff --git a/Numpy using Cython Source code/PythonNumpy Source code/numAddition.py b/Numpy using Cython Source code/PythonNumpy Source code/numAddition.py new file mode 100644 index 0000000..8fc9dd5 --- /dev/null +++ b/Numpy using Cython Source code/PythonNumpy Source code/numAddition.py @@ -0,0 +1,16 @@ +import time +import numpy + +total = 0 +num1 = 1000000000 +arr = numpy.arange(num1, dtype=numpy.int64) + +t1 = time.time() + +for k in arr: + total = total + k +print("Total = ", total) + +t2 = time.time() +t = t2 - t1 +print("%.20f" % t)