Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
359 views
in Technique[技术] by (71.8m points)

python - 我如何获得执行Python程序的时间?(How do I get time of a Python program's execution?)

I have a command line program in Python that takes a while to finish.

(我在Python中有一个命令行程序,需要花一些时间才能完成。)

I want to know the exact time it takes to finish running.

(我想知道完成跑步所需的确切时间。)

I've looked at the timeit module, but it seems it's only for small snippets of code.

(我已经看过timeit模块,但它似乎仅适用于一小段代码。)

I want to time the whole program.

(我想安排整个节目的时间。)

  ask by john2x translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The simplest way in Python:

(Python中最简单的方法:)

import time
start_time = time.time()
main()
print("--- %s seconds ---" % (time.time() - start_time))

This assumes that your program takes at least a tenth of second to run.

(假设您的程序至少需要十分之一秒才能运行。)

Prints:

(印刷品:)

--- 0.764891862869 seconds ---

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...