i new programmer learning python few months. last 2 weeks, had been coding make script search permutations of numbers make magic squares.
finally succeeded in searching whole 880 4x4 magic square numbers sets within 30 seconds. after made different perimeter magic square program. finds out more 10,000,000 permutations want store them part part files. problem program doesn't use processes while working store partial data file, stops searching new number sets. hope make 1 process of cpu keep searching on , others store searched data files.
the following of similar structure magic square program.
while true: print('how many digits want? (more 20): ', end='') ansr = input() if ansr.isdigit() , int(ansr) > 20: ansr = int(ansr) break else: continue filenum = 0 itemcount = 0 def filemaker(): global filenum, itemcount tempstr = '' in permutationlist: itemcount += 1 tempstr += str(sum(i[:3])) + ' : ' + str(i) + ' : ' + str(itemcount) + '\n' filenum += 1 file = open('{0} permutations {1:03}.txt'.format(ansr, filenum), 'w') file.write(tempstr) file.close() numlist = [i in range(1, ansr+1)] permutationlist = [] itemcount = 0 def makepermutlist(numlist, ansr): global permutationlist in numlist: numlist1 = numlist[:] numlist1.remove(i) ii in numlist1: numlist2 = numlist1[:] numlist2.remove(ii) iii in numlist2: numlist3 = numlist2[:] numlist3.remove(iii) iiii in numlist3: numlist4 = numlist3[:] numlist4.remove(iiii) v in numlist4: permutationlist.append([i, ii, iii, iiii, v]) if len(permutationlist) == 200000: print(permutationlist[-1]) filemaker() permutationlist = [] filemaker() makepermutlist(numlist, ansr)
i added from multiprocessing import pool
@ top. , replaced 2 'filemaker()' parts @ end following.
if __name__ == '__main__': workers = pool(processes=2) workers.map(filemaker, ())
the result? oh no. works awkwardly. now, multiprocessing looks difficult me.
anybody, please, teach me something. how should code modified?
well, addressing things bugging me before getting asked question.
numlist = [i in range(1, ansr+1)]
i know list comprehensions cool, please list(range(1, ansr+1))
if need iterable list (which don't need, digress).
def makepermutlist(numlist, ansr): ...
this quite hack. there reason can't use itertools.permutations(numlist,n)
? it's going faster, , friendlier on memory.
lastly, answering question: if looking improve i/o performance, last thing should make multithreaded. don't mean shouldn't it, mean should literally last thing do. refactor/improve other things first.
you need take of top-level code uses globals, apply backspace key it, , rewrite functions pass data around properly. can think using threads. use from threading import thread
, manually spawn threads each unit of i/o rather using multiprocessing.
Comments
Post a Comment