Skip to content

Instantly share code, notes, and snippets.

@mjam03
Created March 27, 2023 21:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mjam03/5d845455c0c52872de842987dd7b05c6 to your computer and use it in GitHub Desktop.
Save mjam03/5d845455c0c52872de842987dd7b05c6 to your computer and use it in GitHub Desktop.
what_exactly_does_'vectorisation'_mean?1
# import numba
from numba import jit
# define our function to profile our compiled code
def find_instr(func, keyword, sig=0, limit=5):
count = 0
# iterate over each of the assembly instructions and check if they match our chosen string
for l in func.inspect_asm(func.signatures[sig]).split('\n'):
if keyword in l or keyword == '':
count += 1
print(l)
if count >= limit:
break
if count == 0:
print('No instructions found')
# apply numba's just-in-time compilation
@jit(nopython=True, fastmath=True)
def arr_sum(arr):
total = 0
for i in range(arr.shape[0]):
total += arr[i]
return total
# define array
arr = np.arange(0, 1_000_000)
# run function (so it compiles at least once)
arr_sum(arr)
# inspect instruction set for additions
find_instr(arr_sum, 'add', limit=5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment