Hi, I’m new to numba and would like to know how I could speed up my code using Numba.
All my currents attempts have resulted in slower execution times so I don’t know what to do next.
Can someone help me with this?
from itertools import product
import numpy as np
def create_coefficients(max_range, functions):
for i in range(2, max_range):
functions.extend(list(product([1, -1], repeat=i + 1)))
def split_complex(functions, res1, res2):
for func in functions[0:(len(functions) // 2)]:
res = np.roots(func)
res1.extend([np.real(ele) for ele in res])
res2.extend([np.imag(ele) for ele in res])
if __name__=="__main__":
functions = []
create_coefficients(15, functions)
res1 = []
res2 = []
split_complex(functions, res1, res2)
@SeppeWouters thank you for posting this. It seems like the post is quite hard to parse so it may be worthwhile to lookup how to format stuff on Discourse. Also, what kind of execution times are you seeing and how are you measuring these?
@SeppeWouters, thank you for updating this example. I tried to run the code locally, but I ran into the following issue:
zsh» python foo
Traceback (most recent call last):
File "foo", line 15, in <module>
create_coefficients(15, functions)
File "foo", line 3, in create_coefficients
functions.extend(list(product([1, -1], repeat=i + 1)))
NameError: name 'product' is not defined
Did you perhaps forget an import statement when copying the code to discourse?
So, this problem is known, because Numba can’t deal with empty lists as arguments as it simply is unable to infer their data type (they don’t exactly have one yet). So I’m not sure how you were able to benchmark the code from the snippet above.
Numba does have it’s own (two actually) list implementation. The more modern one is the numba.typed.List which allows you to specify a datatype, even for an empty list, so that it can be handed into functions. It is also more performant for certain types of use-cases.