Skip to content

Instantly share code, notes, and snippets.

@wktk0
Last active January 25, 2017 05:51
Show Gist options
  • Save wktk0/d4dd612244008c98354592d9cbbe5032 to your computer and use it in GitHub Desktop.
Save wktk0/d4dd612244008c98354592d9cbbe5032 to your computer and use it in GitHub Desktop.
ipythonのマジックコマンドの%timeみたいに一行から複数行の実行時間を計測する

使い方

ファイルに書き出す場合は引数にファイル  

from timer import Timer
# f = open("filename","w")
# f:file option
with Timer(f):
  print("processing details")
import time
class Timer(object):
def __init__(self,f=None):
self.f = f
def __enter__(self):
self.start = time.time()
return self
def __exit__(self, type, value, tb):
print('{} sec'.format(time.time() - self.start),file=self.f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment