Skip to content

Instantly share code, notes, and snippets.

@smileboywtu
Created April 23, 2018 11:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smileboywtu/514df86273618b6bfe506dc94b3259f6 to your computer and use it in GitHub Desktop.
Save smileboywtu/514df86273618b6bfe506dc94b3259f6 to your computer and use it in GitHub Desktop.
calculate python function time
class Timeit(object):
def __init__(self, tag):
self.tag = tag
def __enter__(self):
self.start = time.time()
def __exit__(self, *unused):
self.cost = time.time() - self.start
logger.info("%s took about: %ss" % (self.tag, self.cost))
def exec_profile(tag):
def warpper(func):
def new(*args, **kwds):
with Timeit(tag):
return func(*args, **kwds)
return new
return warpper
@exec_profile("sync device")
def syncer():
with self.mc.cursor() as cursor:
cursor.execute("select hash from waf where hash IS NOT NULL")
self.devices = map(operator.itemgetter("hash"), cursor.fetchall())
logger.info("current device number: {0}".format(self.devices))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment