[Dev] performance of prop vs. mod.prop

Robin Dunn robin at alldunn.com
Thu Sep 9 20:01:58 PDT 2004


Heikki Toivonen wrote:

> 
> I finally decided to measure this, and my tests showed that the second 
> form (mod.prop) is over two times slower. It is still pretty fast, so 
> unless you are doing tens of thousands of iterations it probably does 
> not matter, if even then.
> 

That makes sense since mod.prop is two dictionary lookups (looks in 
globals dictionary to find "mod", and then in mod's dictionary to find 
"prop") vs. just one.  The quickest lookup is locals as the 
byte-compiler optimizes for it, so the best balance between doing good 
imports and speed is to do just "import mod" and then make your own 
optimizations when you need it by assigning to a local.  For example:


	import package.module


	def foo():
		local_prop = package.module.prop
		while looping_tightly():
			# do something with local_prop



-- 
Robin Dunn
Software Craftsman
http://wxPython.org  Java give you jitters?  Relax with wxPython!



More information about the Dev mailing list