
Numba 旨在通过即时 (JIT) 编译将 Python 代码转换为机器码,从而提高性能。@jitclass 装饰器允许用户定义可以被 Numba 编译的类,但正确声明类的属性类型至关重要。特别是在使用枚举 (Enum) 类型时,需要采用特定的方法才能使其与 Numba 兼容。
Numba 无法直接处理标准的 Python enum.Enum 类型。为了在 @jitclass 的 spec 中使用枚举,需要使用 enum.IntEnum。IntEnum 是 Enum 的一个子类,它继承了 int 类型,因此可以转换为 int64,这使得它与 Numba 兼容。
示例代码:
from enum import IntEnum
from numba import int64, string
from numba.experimental import jitclass
class Color(IntEnum):
RED = 1
BLUE = 2
GREEN = 3
spec = [('name', string), ('color', int64)]
@jitclass(spec)
class Paint:
def __init__(self, name, color):
self.name = name
self.color = color
# 示例用法
paint = Paint("MyPaint", Color.RED)
print(paint.name)
print(paint.color)代码解释:
通过继承 enum.IntEnum 并在 @jitclass 的 spec 中将枚举类型声明为 int64,可以有效地在 Numba 中使用枚举类型。这种方法允许 Numba 正确编译包含枚举类型的类,从而提高代码的性能。
虽然目前 Numba 还不支持直接声明自定义类在jitclass的spec中,但对于枚举类型,enum.IntEnum 提供了一个简单有效的解决方案。
以上就是如何在 Numba jitclass spec 中声明 Enum 和自定义类?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号