创建随机二进制文件

创建随机二进制文件

问题描述:

我正在尝试使用python创建随机二进制文件.这是我已经拥有的:

I'm trying to use python to create a random binary file. This is what I've got already:

f = open(filename,'wb')
for i in xrange(size_kb):
    for ii in xrange(1024/4):
        f.write(struct.pack("=I",random.randint(0,sys.maxint*2+1)))

f.close()

但是速度非常慢(在我的3.9GHz SSD磁盘机上,size_kb = 1024的速度为0.82秒).最大的瓶颈似乎是随机int生成(将randint()替换为0会使运行时间从0.82s减少到0.14s).

But it's terribly slow (0.82 seconds for size_kb=1024 on my 3.9GHz SSD disk machine). A big bottleneck seems to be the random int generation (replacing the randint() with a 0 reduces running time from 0.82s to 0.14s).

现在,我知道创建随机数据文件的更有效方法(即dd if =/dev/urandom),但是出于好奇,我正在尝试解决这个问题……是否有明显的方法可以改善此问题?

Now I know there are more efficient ways of creating random data files (namely dd if=/dev/urandom) but I'm trying to figure this out for sake of curiosity... is there an obvious way to improve this?

恕我直言-以下内容完全多余:

IMHO - the following is completely redundant:

f.write(struct.pack("=I",random.randint(0,sys.maxint*2+1)))

绝对不需要使用struct.pack,只需执行以下操作即可:

There's absolutely no need to use struct.pack, just do something like:

import os

with open('output_file', 'wb') as fout:
    fout.write(os.urandom(1024)) # replace 1024 with size_kb if not unreasonably large

然后,如果您需要重新使用文件读取整数,请按struct.unpack键.

Then, if you need to re-use the file for reading integers, then struct.unpack then.

(我的用例正在为单元测试生成一个文件,所以我只需要一个 与其他生成的文件不同的文件.

(my use case is generating a file for a unit test so I just need a file that isn't identical with other generated files).

另一种选择是将UUID4写入文件,但是由于我不知道确切的用例,因此我不确定那是可行的.

Another option is to just write a UUID4 to the file, but since I don't know the exact use case, I'm not sure that's viable.