LoginSignup
13
11

More than 5 years have passed since last update.

Python: Numbaでクラスの高速化

Last updated at Posted at 2019-03-10

前回の続き

numbaでクラスの高速化.
ただし,クラスの継承はできない模様.また,単純にNumbaに対応させようとしても,意外とコードの手直しが必要がなことは難点であるが,一応投稿.

ひとまず,書き方は以下の通り.

spec = [
        ("メンバ変数1", メンバ変数1の型),
        ("メンバ変数2", メンバ変数2の型),
        ....
        ("メンバ変数n", メンバ変数nの型)
    ]

@jitclass(spec)
class ...():
...

from numba import jit, f8, b1, i8, void
from numba import jitclass
import numpy as np
import random

spec = [
        ("width", i8),
        ("array", f8[:,:])
    ]

@jitclass(spec)
class test():
  def __init__(self, w):
    self.width = w
    array = np.array([random.random() for i in range(self.width)])

  def start(self):
    cnt = 0    
    for a in self.array:
      cnt += a

    print(cnt)

if __name__ == "__main__":
  cls = test(10000000)
  cls.start()
13
11
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
11