[MERGE] rand_chars() optimization

John Arbash Meinel john at arbash-meinel.com
Sun Mar 11 13:15:52 GMT 2007


John Arbash Meinel has voted +1 (conditional).
Status is now: Semi-approved
Comment:
Well, if we are going to optimize this one, I have a couple other hints.

First, I like the dictionary.

But appending to a string is expensive, because it has to generate a new 
string each time. I'm not sure how this effects rand_chars(16) which is 
our common case, but it should be a big deal for rand_chars(50).

And then, if you are using a dictionary, you should make it a local 
variable before the loop.

So this is what I would do:

def rand_chars(num):
   _map = _rand_chars_map
   return ''.join([map[b] for b in rand_bytes(num)])

Using a list comprehension, and then doing using string.join should be 
faster.

I tested a list comprehension versus a generator, and 'timeit' says that 
the list is faster. I assume this is because the string can iterate once 
to figure out how long it needs to be, and then again to merge 
everything together. (13us per loop versus 10s) per loop.

So I'm happy with your version, but there are a couple more tricks we 
can apply.

Can you benchmark it with the alternative, to see how much it helps?

For details, see: 
http://bundlebuggy.aaronbentley.com/request/%3C45F3F98F.9030003%40hlabs.spb.ru%3E



More information about the bazaar mailing list