python 3中的快速异或字节 [英] fast XORing bytes in python 3

查看:72
本文介绍了python 3中的快速异或字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要异或 2 个字节的对象.我使用此代码:

I need to xor 2 bytes objects. I use this code:

def bxor(b1, b2): # use xor for bytes
    result = b""
    for b1, b2 in zip(b1, b2):
        result += bytes([b1 ^ b2])
    return result

当字节对象很小时它工作正常,但如果我对大对象(几MB)进行异或则需要很长时间(几个小时).我怎样才能让它更快?

It works fine when the bytes objects are small, but if I xor big objects (a few MB) it takes very long time (a few hours). How can I make it faster?

推荐答案

当对 bytes 对象各有一百万个元素进行异或时,这个循环会创建大约一百万个临时 bytes 对象并将每个字节从一个临时字节复制到下一个,平均大约 50 万次.请注意,字符串也存在完全相同的问题(在许多其他语言中也是如此).字符串解决方案是创建一个字符串部分列表,并在末尾使用 ''.join 来有效地连接它们.你可以用字节做同样的事情:

When XORing bytes objects with one million elements each, this loop creates roughly one million temporary bytes objects and copies each byte, on average, roughly 500 thousand times from one temporary bytes to the next. Note that the exact same problem exists for strings (in many other languages, too). The string solution is to create a list of string parts and use ''.join at the end to concatenate them efficiently. You can do the same thing with bytes:

def bxor(b1, b2): # use xor for bytes
    parts = []
    for b1, b2 in zip(b1, b2):
        parts.append(bytes([b1 ^ b2]))
    return b''.join(parts)

或者,您可以使用可变的 bytearray,因此可以避免该问题.它还允许您不在每次迭代时分配一个新的 bytes 对象,您只需附加 byte/int.

Alternatively, you can use a bytearray which is mutable and can therefore avoid the problem. It also allows you to not allocate a new bytes object on every iteration, you can just append the byte/int.

def bxor(b1, b2): # use xor for bytes
    result = bytearray()
    for b1, b2 in zip(b1, b2):
        result.append(b1 ^ b2)
    return result

如果你想要/需要一个 bytes 对象,你也可以返回 bytes(result).

You can alternatively return bytes(result) if you want/need a bytes object.

这篇关于python 3中的快速异或字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆