Slow bitwise operations

后端 未结 2 1066
臣服心动
臣服心动 2021-01-02 09:08

I am working on a Python library that performs a lot of bitwise operations on long bit strings, and I want to find a bit string type that will maximize its speed. I have tri

2条回答
  •  既然无缘
    2021-01-02 09:35

    What you are trying to test - are these vector operations at all? You are simply trying to compare speeds of 1 operation and there plain python is going to win 'cos it doesn't have to setup numpy arrays or bitarrays.

    How about trying out following?

    x = np.array([random.randrange(2**31)]*1000) 
    y = np.array([random.randrange(2**31)]*1000) 
    
    %timeit x & y # in ipython
    
    %timeit [ a & b for (a,b) in zip(x,y)] # even though x and y are numpy arrays, we are iterating over them - and not doing any vector operations
    

    Interestingly, if

    xxx = [random.randrange(2**31)] * 1000
    yyy = [random.randrange(2**31)] * 1000 
    

    and then

    %timeit [a & b for (a,b) in zip(xxx,yyy)]
    

    pure python lists, iterating over them is faster than iterating over numpy arrays.. a bit counter intuitive. Not sure why.

    Similarly you can try for bitstrings and bitarrays

    Is this what you are looking at?

提交回复
热议问题