Using fixed variables probably doesn't help
John Arbash Meinel
john at arbash-meinel.com
Fri Jun 16 02:57:44 BST 2006
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
I was looking into creating a table of *_kind keys (like
directory_kind='directory') since if you have the same object, you can
save a little bit of time on comparison costs.
To show this is true, you can run:
python -m timeit -s 'x = "directory"; y = "directory"' 'x == y'
or
python -m timeit -s 'x = "directory"; y = x' 'x == y'
And compare the time with:
python -m timeit -s 'x = "directory"; y = "direct" + "ory"' 'x == y'
On my system, the first one takes 0.26us per loop, and the second one
takes 0.29us.
You can also use this if you want:
python -m timeit -s \
'x = intern("directory"); y = intern("direct" + "ory")' \
'x == y'
And that drops back again to 0.26s
But the really interesting thing to me, is that it seems static strings
are always interned. So we don't need to create a separate object for them.
I can also prove this with:
>>> id('test')
393376
>>> id('test')
393376
>>> id('test')
393376
>>> x = 'test'
>>> y = 'test'
>>> id(x), id(y)
(393376, 393376)
I'm not sure what the exact criteria is, when I tried a really long
string it would seem that the first time didn't intern the string, but
the second time would. (The id('longstring') would change twice, and
then become stable).
It might also depend on the interpreter state. After python 2.3
'intern()' doesn't save a reference (I'm guessing it does for short
strings).
Anyway, I'm guessing that we shouldn't spend any time trying to pass
around object references rather than just using 'file' and 'directory'.
The only thing we may want to start doing is calling 'intern()' for
stuff we read from a file.
John
=:->
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.0 (Darwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iD8DBQFEkhAYJdeBCYSNAAMRAoToAJ9MI1o+I6rKiotNDWBmodhYWiMvyACeMFRv
fUeqfbg4Bd0FqjzDj1IrqYc=
=goAJ
-----END PGP SIGNATURE-----
More information about the bazaar
mailing list