[MERGE] rand_chars() optimization

Marius Gedminas marius at pov.lt
Sun Mar 11 13:09:23 GMT 2007


On Sun, Mar 11, 2007 at 03:43:59PM +0300, Dmitry Vasiliev wrote:
> The attached patch makes osutils.rand_chars() faster by precompiling 
> characters mapping and also adds test for uniqueness and benchmark.
> 
> The benchmark results on my system as following:
> 
>  - Old version: 8437ms/8827ms
>  - New version: 5844ms/6219ms
> 
> So the new version is more than 30% faster.
> 
> I guess precompilation overhead is not so big so I haven't added 'on 
> demand' precompilation for now but it's not so hard to do if it will be 
> needed.
...
> === modified file bzrlib/benchmarks/bench_osutils.py
> --- bzrlib/benchmarks/bench_osutils.py
> +++ bzrlib/benchmarks/bench_osutils.py
> @@ -33,3 +33,11 @@
>                  if dirblock[0][1] == '.bzr':
>                      del dirblock[0]
>          self.time(dowalk)
> +
> +
> +class RandCharsBenchmark(Benchmark):
> +
> +    def test_rand_chars(self):
> +        def generate_rand_chars():
> +            items = set(osutils.rand_chars(50) for i in xrange(200000))

Why do you include the creation of a large set of strings in the
benchmark?

> +        self.time(generate_rand_chars)
> 
> === modified file bzrlib/osutils.py
> --- bzrlib/osutils.py
> +++ bzrlib/osutils.py
> @@ -716,15 +716,16 @@
>  
>  
>  ALNUM = '0123456789abcdefghijklmnopqrstuvwxyz'
> +_rand_chars_map = dict((chr(i), ALNUM[i % 36]) for i in range(256))

I suspect it might be a bit faster if you used a string rather than a
dict here:

  _rand_chars_map = ALNUM * (255 / len(ALNUM) + 1)

>  def rand_chars(num):
>      """Return a random string of num alphanumeric characters
> -    
> -    The result only contains lowercase chars because it may be used on 
> +
> +    The result only contains lowercase chars because it may be used on
>      case-insensitive filesystems.
>      """
>      s = ''
>      for raw_byte in rand_bytes(num):
> -        s += ALNUM[ord(raw_byte) % 36]
> +        s += _rand_chars_map[raw_byte]
>      return s


Marius Gedminas
-- 
Writing like a l33t script kiddie hax0r is the absolute kiss of death and
guarantees you will receive nothing but stony silence (or, at best, a heaping
helping of scorn and sarcasm) in return.
	-- ESR (http://www.tuxedo.org/~esr/faqs/smart-questions.html)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : https://lists.ubuntu.com/archives/bazaar/attachments/20070311/c441b956/attachment.pgp 


More information about the bazaar mailing list