LoginSignup
3
2

More than 5 years have passed since last update.

built-in 関数の memoryview でメモリアクセスを効率化

Last updated at Posted at 2018-11-13

built-in 関数なのに知らなかった。大規模なバイト列を利用する場合、 memoryview で無駄なコピーを避けることで、負荷を大幅に下げることが出来る。

$ ipython
In [1]: a = b'a' * int(1e9)

In [2]: %time b = a[:int(1e8)]
CPU times: user 13.7 ms, sys: 24.3 ms, total: 38.1 ms
Wall time: 37.9 ms

In [3]: mv = memoryview(a)

In [4]: %time c = mv[:int(1e8)]
CPU times: user 12 µs, sys: 3 µs, total: 15 µs
Wall time: 20.7 µs

1GBのデータから初めの100MBを取得する処理の場合、約1000倍速。

3
2
0

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
3
2