python native bitwise&的速度比较numpy的

时间:2016-02-01 10:32:07

标签: python numpy timeit

我有一些代码,在这段代码中我需要计算校验和(在传输之前和接收到的数据上)。做一个简单的时间检查,花了相当长的一段时间来计算和检查这个。有210,000个数据包来处理它是有道理的。

通过几个网站阅读(并且弹出几次)numpy bitwise比原生(Fastest bitwise xor between two multibyte binary data variables)更快

同样,我已经研究了lru_cache,因为在大多数情况下我可以得到非常相似的请求。

我试过这个并得到了一些奇怪的结果。

!/usr/bin/env python
#-*- coding: utf-8 -*-

from functools import lru_cache
from numpy import bitwise_and, invert, bitwise_xor
import numpy as np
from timeit import Timer
import random

def checksum1(data):
    '''Checksum is the bytes XOR'ed together, then bit inverted - truncated to a chr'''
    return (~(data[0] ^ data[1] ^ data[2]) & 0xff).to_bytes(1,byteorder='big',signed=False)


@lru_cache(maxsize=128)
def checksum2(data):
    '''Checksum is the bytes XOR'ed together, then bit inverted - truncated to a chr'''
    return (~(data[0] ^ data[1] ^ data[2]) & 0xff).to_bytes(1,byteorder='big',signed=False)


def checksum3(data):
    '''Checksum is the bytes XOR'ed together, then bit inverted - truncated to a chr'''
    return  bitwise_and(invert(bitwise_xor(bitwise_xor(data[0],data[1]),data[2])),255).astype(np.uint8).tobytes()


@lru_cache(maxsize=128)
def checksum4(data):
    '''Checksum is the bytes XOR'ed together, then bit inverted - truncated to a chr'''
    return  bitwise_and(invert(bitwise_xor(bitwise_xor(data[0],data[1]),data[2])),255).astype(np.uint8).tobytes()


if __name__ == "__main__":
    #T = Timer('test()',"from __main__ import test")
    T = Timer('checksum1((random.randint(0,127),0,0))',"import random;from __main__ import checksum1")
    print(T.timeit())
    T = Timer('checksum2((random.randint(0,127),0,0))',"import random;from __main__ import checksum2")
    print(T.timeit())
    T = Timer('checksum3((random.randint(0,127),0,0))',"import random;from __main__ import checksum3")
    print(T.timeit())
    T = Timer('checksum4((random.randint(0,127),0,0))',"import random;from __main__ import checksum4")
    print(T.timeit())
  

py test.py

     

4.10519769108277

     

6.260751157558025

     

10.463237500651697

     

6.182100842095494

这意味着numpy方法很慢&访问lru缓存会提供比它获得的更大的开销。

我做错了什么或这是对的吗?

0 个答案:

没有答案