3
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

pythonでメソッドの処理時間を計測する関数

Last updated at Posted at 2016-10-19

メソッドや関数をまるっと計測する関数です。
python2.7です。

#メソッド
引数の最後に繰り返し回数を入れます

import time
def timer_handler(func, *args):
    #hiracamusさんのご指摘によりtime.time()から修正しました
    start = time.clock() 
    count = args[-1]
    args2 = args[:-1]
    for i in range(count):
        rtn = func(*args2)
    elapsed_time = time.clock() - start
    print("time = " + str(elapsed_time))
    return rtn

#使い方
繰り返し10000回実行した場合の処理時間

def test(a,b,c):
    return pow(a,pow(b,c))

loop = 10000
v = timer_handler(test, 2,3,3, loop)
print v

#出力
time = 0.024491071701
134217728

3
5
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?