[英]Python Elapsed Time as Days, Hours, Minutes, Seconds
I would like to measure the execution time of some piece of code in days , hours, minutes and seconds.我想以天、小时、分钟和秒为单位测量一段代码的执行时间。 This is what I have so far:
这是我到目前为止所拥有的:
import time
start_time = time.time()
# some code
elapsed = time.strftime("%H:%M:%S", time.gmtime(time.time() - start_time))
print(f"Took: {elapsed}")
The problem is that if the code that I am measuring takes longer than 24h, the time displayed overflows and starts from zero again.问题是,如果我测量的代码花费的时间超过 24 小时,则显示的时间会溢出并再次从零开始。 I would like something like this:
我想要这样的东西:
# Example: 12 hours and 34 minutes should be printed as
> Took: 12:34:00
# Example: 26 hours and 3 minutes should be printed as
> Took: 1:02:03:00
The result of time.gmtime(time.time() - start_time)
is not what you seem to think it is. time.gmtime(time.time() - start_time)
的结果并不是你想象的那样。 Instead of being a duration of time it is a point in time.它不是一个持续时间,而是一个时间点。 Let me explain.
让我解释。
The result of time.time()
is the number of seconds since January 1, 1970, 00:00:00 (UTC) at the time of calling. time.time()
的结果是调用时自 1970 年 1 月 1 日 00:00:00 (UTC) 以来的秒数。 Therefore, the statement time.time() - start_time
will produce the number of seconds between the two calls.因此,语句
time.time() - start_time
将产生两次调用之间的秒数。 So far so good.到目前为止,一切都很好。 However, the
time.gmtime
function is interpreting this duration as the number of seconds since January 1, 1970, 00:00:00 (UTC) and formatting the time accordingly.但是,
time.gmtime
function 将此持续时间解释为自 1970 年 1 月 1 日 00:00:00 (UTC) 以来的秒数,并相应地格式化时间。 What you are seeing then is the time portion of the date January 1, 1970, 12:34:00 (UTC).您所看到的是日期 1970 年 1 月 1 日 12:34:00 (UTC) 的时间部分。
I suggest you either use the datetime.timedelta
object and format using that, or as others have suggested, output the duration in seconds or milliseconds.我建议您使用
datetime.timedelta
object 并使用它来格式化,或者按照其他人的建议,output 以秒或毫秒为单位的持续时间。
If you want to format this number yourself, you could use something like this:如果你想自己格式化这个数字,你可以使用这样的东西:
def format_duration(duration):
mapping = [
('s', 60),
('m', 60),
('h', 24),
]
duration = int(duration)
result = []
for symbol, max_amount in mapping:
amount = duration % max_amount
result.append(f'{amount}{symbol}')
duration //= max_amount
if duration == 0:
break
if duration:
result.append(f'{duration}d')
return ' '.join(reversed(result))
You could use datetime:您可以使用日期时间:
from datetime import datetime as dt
start = dt.fromtimestamp(1588432670)
end = dt.now()
elapsed=end-start
print("Took: %02d:%02d:%02d:%02d" % (elapsed.days, elapsed.seconds // 3600, elapsed.seconds // 60 % 60, elapsed.seconds % 60))
Output: Output:
Took: 33:00:21:49
You should try this:你应该试试这个:
import time
start_time = time.time()
...
elapsed_time = time.time() - start_time
days = 0
if elapsed_time >= 86400:
days = int(elapsed_time / 86400)
elapsed = time.strftime("%H:%M:%S", time.gmtime(time.time() - start_time))
if days == 0:
print(f"Took: {elapsed}")
else:
print(f"Took: {days}:{eplased}")
Try using timeit:
尝试使用
timeit:
import timeit
timeit.timeit(<callable function>, number = 100)
Here timeit will call callable function
number
times and give you the average runtime.这里 timeit 将调用
callable function
number
并为您提供平均运行时间。
Time types can only include hours and less units.时间类型只能包括小时和更少的单位。 You should use datetime instead of time as follows:
您应该使用 datetime 而不是 time,如下所示:
from datetime import datetime
start_time = datetime.now()
# some code
elapsed = datetime.now() - start_time)
print(f"Took: {elapsed}")
from datetime import datetime
d1 = datetime(2013,9,1,5,5,4)
d2 = datetime(2013,1,13,3,2,1)
result1 = d1-d2
print ('{} between {} and {}'.format(result1, d1, d2))
This produces following output:这将产生以下 output:
231 days, 2:03:03 between 2013-09-01 05:05:04 and 2013-01-13 03:02:01
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.