performance of script to write very long lines of random chars
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Thu Apr 11 03:47:58 EDT 2013
On Wed, 10 Apr 2013 18:21:51 -0700, gry wrote:
> Dear pythonistas,
> I am writing a tiny utility to produce a file consisting of a
> specified number of lines of a given length of random ascii characters.
> I am hoping to find a more time and memory efficient way, that is still
> fairly simple clear, and _pythonic_.
Here's another option: use string.translate to map random bytes to the
printable ASCII bytes.
import string
all_bytes = ''.join(map(chr, range(256)))
printable = all_bytes[33:127]
n = 127 + len(printable)
extras = all_bytes[127:n]
_deletions = all_bytes[:33] + all_bytes[n:]
_table = string.maketrans(extras, printable)
def random_chars(length, size=1000):
# Make local bindings for speed.
import string, os
table, deletions = _table, _deletions
# Generate random strings.
buffer = []
while True:
while len(buffer) < length:
bytes = string.translate(os.urandom(size), table, deletions)
buffer.extend(bytes)
yield ''.join(buffer[:length])
buffer = buffer[length:]
Then use it like this:
# I want seven lines of twelve char strings.
make = random_chars(12)
for i in range(7):
print next(make)
One thing to be aware of: urandom may run out of entropy, and then it
will slow down a lot. If you don't care about cryptographic randomness,
you could use this instead:
import random
def myrandom(n):
return [random.randint(0, 255) for i in xrange(n)]
although that will actually be slower unless urandom has run out of
entropy.
--
Steven
More information about the Python-list
mailing list