Speed quirk: redundant line gives six-fold speedup

Jack Diederich jack at performancedrivers.com
Thu Aug 25 14:29:36 EDT 2005


On Thu, Aug 25, 2005 at 09:23:09PM +0300, Stelios Xanthakis wrote:
> The explanation is this: hash
> and comparison of objects depends on the state of the memory
> allocator.  A sample case is this:
> 
> 	class A: pass
> 	dummy0=47  # comment this to get a different result for min
> 	a=A()
> 	b=A()
> 	print min (a, b)
> 
> the result of 'min' is not only non-deterministic but also depends
> on whether other things have been allocated before.  The same
> thing can happen for 'dictionary.keys()' if the keys are objects
> and 'iterate-over-set' when the set contains objects.
> 
> In the sudoku solver, there is a min (number, object) which is
> probably what's affected by the extistance of the dummy variable.
> Now, in sudoku puzzles some times the algorithm has to suppose

Doh, I feel silly.  Without an 'import random' in the program I
assumed it was deterministic.  I would have also expected the
comparison to be a TypeError, and it sometimes is.

>>> mynum = 7
>>> myob = object()
>>> cmp(mynum, myob)
-1
>>> mynum.__cmp__(myob)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: int.__cmp__(x,y) requires y to be a 'int', not a 'object'


Thanks for the explanation,
-jackdied



More information about the Python-list mailing list