Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NumPy Using Cython #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import array5_boundCheck
import numpy
arr = numpy.arange(1000000000, dtype=numpy.int)
array5_boundCheck.do_calc(arr)
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
@@ -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()]
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import array4_indexing
import numpy
arr = numpy.arange(1000000000, dtype=numpy.int)
array4_indexing.do_calc(arr)
Original file line number Diff line number Diff line change
@@ -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)
8 changes: 8 additions & 0 deletions Numpy using Cython Source code/Indexing Source code/setup.py
Original file line number Diff line number Diff line change
@@ -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()]
)
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
@@ -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()]
)
Original file line number Diff line number Diff line change
@@ -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)