numba的协程

编程入门 行业动态 更新时间:2024-10-24 15:23:24
本文介绍了numba的协程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在研究需要快速协程的东西,我相信numba可以加快我的代码的速度。

I'm working on something that requires fast coroutines and I believe numba could speed up my code.

这是一个愚蠢的例子:一个将输入平方的函数,

Here's a silly example: a function that squares its input, and adds to it the number of times its been called.

def make_square_plus_count(): i = 0 def square_plus_count(x): nonlocal i i += 1 return x**2 + i return square_plus_count

您甚至不能 nopython = False 这样做,大概是由于非本地关键字。

You can't even nopython=False JIT this, presumably due to the nonlocal keyword.

但是如果您使用课程,则不需要非本地相反:

But you don't need nonlocal if you use a class instead:

def make_square_plus_count(): @numba.jitclass({'i': numba.uint64}) class State: def __init__(self): self.i = 0 state = State() @numba.jit() def square_plus_count(x): state.i += 1 return x**2 + state.i return square_plus_count

这至少有效,但是如果您会执行 nopython = True 。

This at least works, but it breaks if you do nopython=True.

是否存在可以使用编译的解决方案nopython = True ?

推荐答案

如果您要使用状态类,则可以还使用方法而不是闭包(应该是非python编译的):

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]

但是时间显示,这实际上比纯python闭包实现要慢tion。我希望只要您不使用 nonlocal numpy-arrays或在您的方法(或闭包)中对数组进行操作,效率就会降低!

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!

更多推荐

numba的协程

本文发布于:2023-11-23 10:02:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1621039.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:numba

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!