cpdef float iterate_list(a_list):
cdef double count = 0
cdef int i, j
for i in range(len(a_list)):
internal_list = a_list[i]
for j in range(len(internal_list)):
count += internal_list[j]
print(count)
return count
To: cpdef float iterate_list(list a_list):
cdef double count = 0
cdef double val = 0
cdef list ilist
for ilist in a_list:
for val in ilist:
count += val
print(count)
return count
Speeds up the iterate_list function an order of magnitude. On my PC: In [9]: %timeit list_cy.iterate_list(a_list)
1000000.0007792843
1000000.0007792843
1000000.0007792843
1000000.0007792843
1000000.0007792843
1000000.0007792843
1000000.0007792843
1000000.0007792843
385 ms ± 6.15 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
In [10]: %timeit list_cyo.iterate_list(a_list)
1000000.0007792843
1000000.0007792843
1000000.0007792843
1000000.0007792843
1000000.0007792843
1000000.0007792843
1000000.0007792843
1000000.0007792843
2.71 s ± 182 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
(yeah, I kept the prints that the code has)Where list_cy is the fixed code and list_cyo is the original code. Even then, iterating over a list of lists is _definitely not_ the optimal way you'd face a problem of this kind. Numpy arrays and memoryviews would be the correct tool to use.