gpt4 book ai didi

Python:获取当前行运行时间

转载 作者:太空宇宙 更新时间:2023-11-03 18:48:33 24 4
gpt4 key购买 nike

Python中如何获取当前正在运行的脚本 block ,运行时

print a()             #takes couple of seconds, 3s
print b() #takes couple of seconds, 3s
pass
print c() #takes couple of seconds, 4s
print d(), \
t,y,u,i,o #takes couple of seconds, 4s
print z

例如,了解(报告)第二个 7 号正在运行的内容。

report(7s): print c()

编辑:

我们保留上述内容是为了证明所给出的评论是合理的;)但是下面我们更详细地描述我们的问题。

Python 代码正在逐行执行(或者更好的是逐 block 执行)。参见示例:

code.py中:

for i in xrange(10000000):                #assume this will take some seconds
pass

print 'something'
#another time consuming job
for j in xrange(10000000): #assume this will also take some seconds
pass

print 'another thing'

我们正在考虑建立一个时间线程,每 5 秒采样一次以打印(即报告)我们在运行时代码中的位置。因此,每 5 秒打印一次执行期间发生的情况。

输出示例:

>>> 00:05 in progress: "for i in xrange(10000000):..."
>>> 00:10 in progress: "for i in xrange(10000000):..."
>>> 00:15 in progress: "for j in xrange(10000000):..."
...

最佳答案

def fowia(signal, frame):
print frame.f_lineno


import signal
signal.signal(signal.SIGHUP, fowia)

for i in xrange(10000000):
for k in xrange(100000000):
pass

print 'something'
#another time consuming job
for j in xrange(10000000):
pass
print 'another thing'

要使用这个简单的示例,请启动它运行,然后从另一个 shell 查找进程的 pid 号。然后向进程发送 HUP 信号。它将打印当前行号。请参阅http://docs.python.org/2/library/inspect.html有关可以使用的其他程序源自省(introspection)方法的列表

fowia = 顺便说一句,找出我在哪里:)

关于Python:获取当前行运行时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18911093/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com