some OT: how to solve this kind of problem in our program?
bearophileHUGS at lycos.com
bearophileHUGS at lycos.com
Sun Dec 24 06:08:10 EST 2006
Paul McGuire:
> This little framework takes about 4.5 seconds, but with
> psyco, cuts down to about 1.3 seconds.
>
> st = time.time()
> result = []
> for p in permutations(range(1,10)):
> aresult = func(p)
> if aresult is not None and aresult not in result:
> result.append(aresult)
>
> et=time.time()
> print 'time elapsed: %.4f s' % (et-st)
> for [[a0, a1, a2], [b0, b1, b2], [c0, c1, c2]] in result:
> print ' %0d %0d %0d %0d' % (a0, b0, c0, d0)
> print '--- + --- + --- = ---'
> print ' %0d%0d %0d%0d %0d%0d %0d' %(a1, a2, b1, b2, c1,c2, d1)
> print
If you want to more speed, put long loops always inside functions, not
inside the main body:
def main():
st = clock()
result = []
for p in permutations(range(1, 10)):
aresult = func(p)
if aresult is not None and aresult not in result:
result.append(aresult)
et = clock()
print 'time elapsed: %.4f s' % (et-st)
for [[a0, a1, a2], [b0, b1, b2], [c0, c1, c2]] in result:
print ' %0d %0d %0d %0d' % (a0, b0, c0, d0)
print '--- + --- + --- = ---'
print ' %0d%0d %0d%0d %0d%0d %0d' %(a1, a2, b1, b2,
c1,c2, d1)
print
main()
If you have a bit of time you can test the speed of this code on your
computer.
Bye,
bearophile
More information about the Python-list
mailing list