Skip to content

Commit

Permalink
Merge pull request numba#7682 from rishi-kulkarni/master
Browse files Browse the repository at this point in the history
performance improvements to np.full and np.ones
  • Loading branch information
sklam authored Jan 21, 2022
2 parents 6043825 + 540c910 commit 14b170a
Showing 1 changed file with 24 additions and 16 deletions.
40 changes: 24 additions & 16 deletions numba/np/arrayobj.py
Original file line number Diff line number Diff line change
Expand Up @@ -3789,8 +3789,9 @@ def numpy_full_nd(context, builder, sig, args):

def full(shape, value):
arr = np.empty(shape, type(value))
for idx in np.ndindex(arr.shape):
arr[idx] = value
arr_flat = arr.flat
for idx in range(len(arr_flat)):
arr_flat[idx] = value
return arr

res = context.compile_internal(builder, full, sig, args)
Expand All @@ -3803,8 +3804,9 @@ def numpy_full_dtype_nd(context, builder, sig, args):

def full(shape, value, dtype):
arr = np.empty(shape, dtype)
for idx in np.ndindex(arr.shape):
arr[idx] = value
arr_flat = arr.flat
for idx in range(len(arr_flat)):
arr_flat[idx] = value
return arr

res = context.compile_internal(builder, full, sig, args)
Expand All @@ -3816,8 +3818,9 @@ def numpy_full_like_nd(context, builder, sig, args):

def full_like(arr, value):
arr = np.empty_like(arr)
for idx in np.ndindex(arr.shape):
arr[idx] = value
arr_flat = arr.flat
for idx in range(len(arr_flat)):
arr_flat[idx] = value
return arr

res = context.compile_internal(builder, full_like, sig, args)
Expand All @@ -3830,8 +3833,9 @@ def numpy_full_like_nd_type_spec(context, builder, sig, args):

def full_like(arr, value, dtype):
arr = np.empty_like(arr, dtype)
for idx in np.ndindex(arr.shape):
arr[idx] = value
arr_flat = arr.flat
for idx in range(len(arr_flat)):
arr_flat[idx] = value
return arr

res = context.compile_internal(builder, full_like, sig, args)
Expand All @@ -3843,8 +3847,9 @@ def numpy_ones_nd(context, builder, sig, args):

def ones(shape):
arr = np.empty(shape)
for idx in np.ndindex(arr.shape):
arr[idx] = 1
arr_flat = arr.flat
for idx in range(len(arr_flat)):
arr_flat[idx] = 1
return arr

valty = sig.return_type.dtype
Expand All @@ -3859,8 +3864,9 @@ def numpy_ones_dtype_nd(context, builder, sig, args):

def ones(shape, dtype):
arr = np.empty(shape, dtype)
for idx in np.ndindex(arr.shape):
arr[idx] = 1
arr_flat = arr.flat
for idx in range(len(arr_flat)):
arr_flat[idx] = 1
return arr

res = context.compile_internal(builder, ones, sig, args)
Expand All @@ -3872,8 +3878,9 @@ def numpy_ones_like_nd(context, builder, sig, args):

def ones_like(arr):
arr = np.empty_like(arr)
for idx in np.ndindex(arr.shape):
arr[idx] = 1
arr_flat = arr.flat
for idx in range(len(arr_flat)):
arr_flat[idx] = 1
return arr

res = context.compile_internal(builder, ones_like, sig, args)
Expand All @@ -3886,8 +3893,9 @@ def numpy_ones_like_dtype_nd(context, builder, sig, args):

def ones_like(arr, dtype):
arr = np.empty_like(arr, dtype)
for idx in np.ndindex(arr.shape):
arr[idx] = 1
arr_flat = arr.flat
for idx in range(len(arr_flat)):
arr_flat[idx] = 1
return arr

res = context.compile_internal(builder, ones_like, sig, args)
Expand Down

0 comments on commit 14b170a

Please sign in to comment.