Hi! Today I’m writing about ONPyTimer, which is a small (really, ridiculously small) python utility I wrote today.

ONPyTimer provides a way to keep track of the execution time in a Python script. Its exposed method (“check”) allows you to get the current time, but also flag it (basically, give this moment a name). When you use a flag, you can then use it to compare the *new* current time to that *flagged* time. For example:

# Set FLAG A here (time = 1)
# code here
# Get time from FLAG A (time = 13) ==> time from FLAG A is 12


Pretty simple, uh?


UPDATE: Here is a real life example:

ONPyTimer.check(flag='A') # will show current time and flag name
ONPyTimer.check(flags=['A']) # will show time diff from flag 'A'
ONPyTimer.check(flag='B', flags=['A']) # will show new flag name and time diff
... # from flag 'A'
ONPyTimer.check(flags=['A', 'B']) # will show time diff from flags 'A' and 'B'


I needed something like it, so I coded it. It’s now posted on my GitHub account here.

The README provides some examples, but believe me, is really really simple. Just use ONPyTimer.check(), pass in some flag parameters, and your done.

Hope you find it useful!


UPDATE: I’ve added support for file output! :) If you don’t want it to print data into stdout (as print command does) you can use the ‘output’ parameter in the check method and pass an opened file (or file-like object, like StringIO), for example:

ONPyTimer.check(flags=['A'], output=f) # will write time diff from flag 'A' to file 'f'

This is very useful for scripts like socket servers and concurrent processing.



1 Comment so far

Comments are closed.