Interesting performance about attribute lookup

John Arbash Meinel john at arbash-meinel.com
Sat Oct 25 15:50:37 BST 2008


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

I was looking at timing to see how long it takes to iterate over a set
and build up a list, and I found something interesting (at least to me):


TIME_SETUP="python -m 'timeit' -s 'orig_set = set(range(100000))'"
$ TIME_SETUP \
"for val in orig_set:
    pass
"
2.76 msec

$ TIME_SETUP \
"copy = []
for val in orig_set:
  copy.append(val)
"
15.6 msec

$ TIME_SETUP \
"copy = []
app = copy.append
for val in orig_set:
    app(val)
"
8.22 msec

so it seems that looking up the "copy.append" attribute 100,000 times
takes 7.4 msec (15.6-8.22), while actually doing the append only takes
5.46 msec (8.22-2.76).

I know we've done work in the past by getting rid of attribute lookups,
I just didn't realize that they were more expensive than appending items
to a list(). (This is python2.5 on windows, YMMV.)

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

iEYEARECAAYFAkkDMj0ACgkQJdeBCYSNAAP5EwCeNKYzlgBWk9O7pybkizTBPYrm
JcYAnj1KDP4PDN/0/hICMeaj/H9Ffa16
=Q/uq
-----END PGP SIGNATURE-----



More information about the bazaar mailing list