1 / 4

Python – May 26

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”)

reaves
Download Presentation

Python – May 26

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Python – May 26 • Persistent objects • Experimenting with time

  2. 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()

  3. 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)

  4. 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?

More Related