Performance comment (oddity with set.update())

John Arbash Meinel john at arbash-meinel.com
Tue Apr 22 22:49:10 BST 2008


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I was just debugging some performance issues, and I came across something funny.

Specifically, calling 'set.update()' with a generator that returns nothing is
slower than building it into a list and then updating.

With python 2.4.3:

$ python -m timeit -s 'x = set(range(10000))' 'x.update([])'
1000000 loops, best of 3: 0.296 usec per loop
$ python -m timeit -s 'x = set(range(10000))' 'x.update(y for y in [])'
1000000 loops, best of 3: 0.837 usec per loop
$ python -m timeit -s 'x = set(range(10000))' 'x.update([y for y in []])'
1000000 loops, best of 3: 0.462 usec per loop



With 2.5.1 (on a different machine)
$ python -m timeit -s 'x = set(range(10000))' 'x.update([])'
1000000 loops, best of 3: 0.265 usec per loop
$ python -m timeit -s 'x = set(range(10000))' 'x.update(y for y in [])'
1000000 loops, best of 3: 0.717 usec per loop
$ python -m timeit -s 'x = set(range(10000))' 'x.update([y for y in []])'
1000000 loops, best of 3: 0.39 usec per loop


Anyway, might be something to keep in mind for some of our inner functions.

John
=:->
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (Cygwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkgOXVYACgkQJdeBCYSNAAONiQCeKrraseD0LTrol4H6LLEAVud6
Np4An2zNR3txHtzNXWop3LH82PDD6dj2
=R2H2
-----END PGP SIGNATURE-----



More information about the bazaar mailing list