Coroutines in numba

六月ゝ 毕业季﹏ 提交于 2019-12-05 11:47:55

If you're going to use a state-class anyway you could also use methods instead of a closure (should be no-python compiled):

import numba

@numba.jitclass({'i': numba.uint64})
class State(object):
    def __init__(self):
        self.i = 0

    def square_plus_count(self, x):
        self.i += 1
        return x**2 + self.i

square_with_call_count = State().square_plus_count  # using the method
print([square_with_call_count(i) for i in range(10)])
# [1, 3, 7, 13, 21, 31, 43, 57, 73, 91]

However timings show that this is actually slower than a pure python closure implementation. I expect that as long as you don't use nonlocal numpy-arrays or do operations on arrays in your method (or closure) this will be less efficient!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!