Speeding up a script -- looking for ideas

William wilk-spamout at flibuste.net
Sun Oct 20 04:57:54 EDT 2002


Richard Bow <donkan7 at yahoo.com> writes:

> I'm hoping to get some hints for significantly speeding up the below 
> script. I've found 43 integers between 1 and 1,000,000 that satisfy the 
> condition, and would like to push on toward 10,000,000 in hopes of finding 
> an integer for which there are 3 pairs. Of course, another reason for 
> asking is to learn more Python from you guys.

hi,

without looking at your algorithme, it's typical think where psyco will
help you :

without psyco :

[(13832, 2, 24), (13832, 18, 20)]

Time's up! 16.6103349924 seconds have elapsed
EEEEEEEEEnnnnnnnnnnddddddddd

with psyco :

[(13832, 2, 24), (13832, 18, 20)]

Time's up! 2.99753201008 seconds have elapsed
EEEEEEEEEnnnnnnnnnnddddddddd

http://psyco.sourceforge.net/

i try with python2.2 and last psyco CVS on my pII350

import psyco

start = 10000
end   = 20000
 

def f():
    for n in range(start, end + 1):
        crn = int(n**(1/3.0)) + 2 #  because of float probs, add 2 to be sure 
        list = []                 #  crn is large enough 

        for a in range(1,crn):
            a3 = a ** 3 
            for b in range(a,crn): 
                b3 = b ** 3 
                if a3 + b3 == n:                        
                    list = list + [(n,a,b)]
                    if len(list) >= 2:
                    # e.g. [(1729, 1, 12), (1729, 9, 10)] and 
                            # [(994688, 29, 99), (994688, 60, 92)]

                        print list
                        print
psyco.bind(f)
import time
t1= time.time()
f()                    
t2 = time.time()
print "Time's up!", t2 - t1, "seconds have elapsed"

print "EEEEEEEEEnnnnnnnnnnddddddddd"

-- 
William Dodé - flibuste.net
http://wikipython.tuxfamily.org



More information about the Python-list mailing list