python时间变量_Python时间变量不变

我修改了一些在stackoverflow上发现的here代码,但是我得到了一些奇怪的行为。有人能看出我的错误吗?在import atexit

from time import clock

line = "="*10 + ">>>"

def secondsToStr(s):

return "{0}.{1}ms".format(round(s*1000), round(s*1000000))

##

# @brief Starts a timer. If the program crashes, the end function will be called anyway.

def start():

atexit.register(stop)

print(line,"Start time measurement")

startTime = clock()

print("new start time:", startTime)

##

# @brief Needs to be called after start(). Prints the time passed since start() was called.

def stop():

end = clock()

elapsed = end-startTime

print(startTime, end) # Inserted for debugging

print(line, "Ellapsed time:", secondsToStr(elapsed))

atexit.unregister(stop)

def now():

return secondsToStr(clock())

startTime = clock()

其思想是调用start()和stop()来测量程序在这两个调用之间花费的时间。奇怪的是,startTime没有改变。因此,stop()的每个调用都给出了自start()第一次调用以来的过去时间。以下是输出示例:

^{pr2}$

为什么startTime永远不变?它应该在每次调用start()时获得一个新值。在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值