Short Notes on Python

From PaskvilWiki
Revision as of 03:20, 2 September 2012 by Admin (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Timing, and memory, on Linux

Timing

On Linux, it's safer to use time.time()

import time
t = time.time()
# do some stuff
print "stuff took %1.3f", time.time() - t, "seconds"

On Windows, AFAIK, it's safer to use time.clock()

Memory

For me, the following does a good job getting memory usage (in kB) on Linux:

import resource
print resource.getrusage(resource.RUSAGE_SELF).ru_maxrss

Since resource is standard package, it should work on Windows too, but I don't know if it does, or what units are used if it works.