To count number of quadruplets with sum = 0
bearophileHUGS at lycos.com
bearophileHUGS at lycos.com
Sat Mar 17 14:43:41 EDT 2007
n00m:
> i have no NumPy to test it...
> without Psyco Anton's code is the winner: ~48sec vs ~58sec of my code
> But with Psyco my runtime is ~28sec; Anton's - ~30sec (PC: 1.6 ghz,
> 512 mb)
> Not so bad.. keeping in mind that 256000 billions quadruplets to
> check :)
I have oiled it a bit, you can try the speed of this too (for dicts in
is always faster than has_key).
from collections import defaultdict
import time, psyco
def main():
sch = 0
q,w,e,r = [],[],[],[]
h = defaultdict(int)
datafile = file("m1000.txt")
datafile.next()
xrows = (map(int, line.split()) for line in datafile)
q, w, e, r = zip(*xrows)
for x in q:
for y in w:
h[x+y] += 1
for x in e:
for y in r:
if -x-y in h:
sch += h[-x-y]
print sch
t = time.clock()
psyco.full()
main()
print round(time.clock() - t, 2), "secs"
More information about the Python-list
mailing list