Python name lookups

jepler at unpythonic.net jepler at unpythonic.net
Mon Oct 10 18:47:02 EDT 2005


On Mon, Oct 10, 2005 at 03:02:30PM -0700, Dave wrote:
> Hello All,
> 
> As far as I understand, Python deals with a lot of
> string objects, i.e. it looks up all names. Is there a
> way to find out how many name lookup operations take
> place in a Python program? Is it the name lookup
> operation or hash operation that degrades performance?
> What function does Python use for name lookup
> operations? Is it the lookdict function in
> Objects/dictobject.c?

lookdict_string is used for most lookups of the form
	obj.attr
because they are never found to have non-string keys
entered or searched.

Furthermore, most of these string keys are "interned",
which I believe makes the check
        if (ep->me_key == NULL || ep->me_key == key)
                return ep;
return a result without comparing the contents of the strings.

Using gdb, I tried to get a rough estimate of how often pystone calls
_PyString_Eq while running a benchmark program, compared to how many
times it calls lookdict_string.  Here's a simplified gdb session:
	$ gdb ./python
	(gdb) break _PyString_Eq
	(gdb) ignore 1 1000000000
	(gdb) break lookdict_string
	(gdb) ignore 2 1000000000
	(gdb) r Lib/test/pystone.py 200
	Pystone(1.1) time for 200 passes = 0.13
	This machine benchmarks at 1538.46 pystones/second
(the low score is because of the overhead of gdb breaking on those functions)
	(gdb) info b
	Num Type           Disp Enb Address    What
	1   breakpoint     keep y   0x0807e126 in _PyString_Eq
		breakpoint already hit 4040 times
	2   breakpoint     keep y   0x0807573c in lookdict_string
		breakpoint already hit 57254 times
So there seem to be less than .1 string comparisons per name lookup.

In fact, for 200 or for 50000 passes, there are still just 4040 calls to
_PyString_Eq, which would give .003 string comparisons per name lookup,
assuming the numbers for lookdict_string scale with the number of passes.

Jeff
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20051010/ef08e129/attachment.sig>


More information about the Python-list mailing list