Collision of rotated rectangles without pygame

Ian ian.g.kelly at gmail.com
Tue Dec 7 21:51:01 EST 2010


On Dec 7, 4:11 pm, Steve Holden <st... at holdenweb.com> wrote:
> >>> timeit.timeit(fm)
> 0.58099985122680664
> >>> timeit.timeit(fd)
> 0.55200004577636719
>
> Of course it's possible that the random number generation is dominating,

I think that it is.  Moving the random number generation out into
setup:

>>> t1 = timeit.Timer("sin(x.next())", "from math import sin, radians; import random; x = iter([random.random() for i in xrange(1000000)])")
>>> t1.timeit(1000000)
0.45154733352978838

>>> t2 = timeit.Timer("d[x.next()]", "import math, random; x = iter([random.randrange(360) for i in xrange(1000000)]); d = dict((i, math.sin(math.radians(i))) for i in xrange(360))")
>>> t2.timeit(1000000)
0.21765364033694823

Of course, the dict approach assumes that all angles will be an
integer number of degrees.  One could add precision, but looking up
specific float values in a dict is dicey, so then one has to switch to
decimal math, and that's going to add extra overhead -- probably
enough to tip the scales in favor of math.sin.

Cheers,
Ian



More information about the Python-list mailing list