如何在python中启动秒表[重复]
Posted
技术标签:
【中文标题】如何在python中启动秒表[重复]【英文标题】:How to start stopwatch in python [duplicate] 【发布时间】:2021-08-14 13:30:00 【问题描述】:我想使用秒表来计时我的代码,但我需要帮助。这是我的代码:
import time
timer = 0
alreadyout = 'no'
eligable = 'no'
entered = 'no'
if alreadyout == 'no' and eligable == 'no':
answer = str(input("type to 100 letters as fast as you can"))
if len(answer) == 100:
eliable = 'yes'
entered = 'yes'
alreadyout = 'yes'
while entered == 'no':
time.sleep(1)
timer = timer + 1
print(timer)
【问题讨论】:
【参考方案1】:您可以使用time.time()
为您的代码计时。这是一个例子
import time
start = time.time()
# <code to time>
end = time.time()
print(f"Time taken to run the code was end-start seconds")
【讨论】:
以上是关于如何在python中启动秒表[重复]的主要内容,如果未能解决你的问题,请参考以下文章
每次python中的秒表达到8的倍数(8、16、24、32等...)时,如何使变量的值相加