import random
import time
class Stopwatch:
def __enter__(self):
self.start = time.time()
return self # ここで自分自身を返す
def __exit__(self, exc_type, exc_value, traceback):
self.end = time.time()
self.interval = self.end - self.start # 計測時間を属性に保存
with Stopwatch() as sw:
for _ in range(1024):
k = random.randint(0,1024) * random.randint(0,1024)
# print(k) # 出力を消すと正確な処理時間が測れる
print(f"処理時間: {sw.interval:.6f} 秒")
To embed this project on your website, copy the following code and paste it into your website's HTML: