python - Faster to add all items to an array then write the array to file, or faster to write the item to a file and then add to array one at a time? -


so right have (in python 2.7):

if y == ports[0]:    array1.append(x)  elif y == ports[1]:    array2.append(x)  elif y == ports[2]:    array3.append(x)  elif y == ports[3]:    array4.append(x)  else:    array5.append(x)  x in array1:     target=open('array1.csv', 'a')     target.write(x + ",\n")     target.close()     print "added ip address " + x + " array1.csv\n"  x in array2:  target=open('array2.csv', 'a')  target.write(x + ",\n")  target.close()  print "added ip address " + x + " array2.csv\n"  x in array3:  target=open('array3.csv', 'a')  target.write(x + ",\n")  target.close()  print "added ip address " + x + " array3.csv\n"  x in array4:   target=open('array4.csv', 'a')   target.write(x + ",\n")   target.close()   print "added ip address " + x + " array4.csv\n"  x in array5:   target=open('array5.csv', 'a')   target.write(x + ",\n")   target.close()   print "added ip address " + x + " array5.csv\n" 

would program finish quicker if did:

if y == ports[0]:     array1.append(x)     target=open('array1.csv', 'a')     target.write(x + ",\n")     target.close()     print "added ip address " + x + " array1.csv\n" elif y == ports[1]:     array2.append(x)     target=open('array2.csv', 'a')     target.write(x + ",\n")     target.close()     print "added ip address " + x + " array2.csv\n" elif y == ports[2]:     array3.append(x)     target=open('array3.csv', 'a')     target.write(x + ",\n")     target.close()     print "added ip address " + x + " array3.csv\n" elif y == ports[3]:     array4.append(x)     target=open('array4.csv', 'a')     target.write(x + ",\n")     target.close()     print "added ip address " + x + " array4.csv\n" else:     array5.append(x)     target=open('array5.csv', 'a')     target.write(x + ",\n")     target.close()     print "added ip address " + x + " array5.csv\n" 

or see difference @ all? or perhaps there third way quicker? matter when item written list?

that barely matters. what’s more important not reopening file:

with open('array1.csv', 'a') target:     x in array1:         target.write(x + ",\n")         print "added ip address " + x + " array1.csv\n" 

also, don’t know if applies here, the csv module does exist.


Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -