Performance issue in python with nested loop

后端 未结 2 1713
醉话见心
醉话见心 2021-01-27 09:41

I was able to improve a code written in python a lot with numpy because of the dot product. Now I still have one part of the code which is still very slow. I still don\'t unders

2条回答
  •  爱一瞬间的悲伤
    2021-01-27 10:10

    Before worrying about multiprocessing, try using what plain numpy offers.

    First, make sure ws are numpy arrays, not lists of some such. Then

    cv1 = numpy.ndarray.sum(numpy.absolute(numpy.subtract(ws[x1], ws[x2])))
    if cv1 == 0:
       f11 += 1
    

    becomes f11 = np.nonzero(ws[x1] == ws[x2]).

    Do the same for the rest of the code, and you'll be able to see more structure: np.product is just * and so on.

    Then, re[x1][x2][x3] is not how you normally index numpy arrays, use re[x1, x2, x3]. This alone would save you quite a bit of time and memory allocations.

    Once this is done, look if you can actually vectorize the expressions, i.e. use numpy all-array operations instead of plain python loops (it's likely that you can, but it's too hard to see with the current state of the code snippet).

提交回复
热议问题