40 likes | 58 Views
Python – May 26. Persistent objects Experimenting with time. Persistence. You may find it helpful to write a large object to a binary file. import cPickle file = open(“outputfile”, “wb”) cPickle.dump (aLotOfData, file) file.close() file2 = open(“outputfile”, “rb”)
E N D
Python – May 26 • Persistent objects • Experimenting with time
Persistence • You may find it helpful to write a large object to a binary file. import cPickle file = open(“outputfile”, “wb”) cPickle.dump(aLotOfData, file) file.close() file2 = open(“outputfile”, “rb”) newlist = cPickle.load(file2) file2.close()
Time module import time • time.localtime() returns a list containing: • Year, month, day, hour, min, sec, day#, Julian day • time.clock() • Returns 0 the first time you call it • Otherwise returns a float # of seconds since first call. • Good to subtract 2 consecutive calls to time a segment of code. • time.sleep(# of seconds)
Timing quick event • How long does it take to add an element to a list? t1 = time.clock() # loop to add 100 elements t2 = time.clock() print (t2-t1)/100 What’s wrong with this approach?