0
2

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 3 years have passed since last update.

Pythonで差分計測用のStopWatchを作る

Last updated at Posted at 2020-07-22

すでに多くの記事でPythonを使った処理時間計測方法が提示されているので、詳しい内容は割愛。
自分で使いやすいように、下記の通りStopWatchを作ってみる。

PCで及びPython処理時間計測する事自体にあまり精度が期待できないので、ある程度自分が信じる精度を決めておけば今後の計測において役にたつかなぁと思い、何度か実験しましたが ms 程度の精度で十分な気もするので少数第4位くらいまでで経過時間を戻す仕様としてみました。
何点か同時に測定できるように、識別子付きのStopWatchとなっております。

StopWatch.py
import time

class StopWatch():
    def __init__(self, name=None):
        self.name = name
        self.start_time = 0
        self.stop_time = 0

    def start(self):
        self.start_time = time.perf_counter()

    def stop(self):
        self.stop_time = time.perf_counter()
        tm = self.stop_time - self.start_time
        return(round( tm, 4))

    def clear(self):
        self.start_time = 0

    def isName(self):
        return self.name


if __name__ == "__main__":
    sw1 = StopWatch("A")
    sw2 = StopWatch("B")    
    sw1.start()
    time.sleep(1)
    
    print(sw1.stop())
    sw2.start()
    for i in range(1000000):
        i=i+0.01
    print(sw1.name + ":" + str(sw1.stop()))
    print(sw2.name + ":" + str(sw2.stop()))    


0
2
0

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
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?