LoginSignup
1
1

More than 3 years have passed since last update.

リングバッファのBit演算化(高速化)

Posted at

経緯

リングバッファを使用する際に余剰を使用してインデックスを計算していた。職場のプログラマの方に「ビット演算でもできるよ」と教えてもらったのでここにメモしておく。

Code(Python)

ring_buffer.py
# coding:utf-8
'''
Ring Bufferがビット演算で早くなるか実験
'''
import time
import numpy as np

def main():
    ring_buff = np.array([i for i in range(1024)])

    #余剰演算ver
    start = time.time()

    for cnt in range(102400):
        index = cnt % 1024
        ring_buff[index] = cnt

    elapsed_time = time.time() - start
    print ("余剰ver   :{0}".format(elapsed_time) + "[sec]")    

    #bit演算ver
    start = time.time()

    for cnt in range(102400):
        index = cnt & 0b1000000000
        ring_buff[index] = cnt

    elapsed_time = time.time() - start
    print ("bit ver   :{0}".format(elapsed_time) + "[sec]")

if __name__ == '__main__':
    main()
1
1
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
1
1