Home > Backend Development > Python Tutorial > How to implement timing in python

How to implement timing in python

Release: 2019-07-06 09:02:12
Original
18671 people have browsed it

How to implement timing in python

The time module can be used in Python to implement the timing function:

import time
print('按下回车开始计时,按下 Ctrl + C 停止计时。')
while True:
    try:
        input() # 如果是 python 2.x 版本请使用 raw_input() 
        starttime = time.time()
        print('开始')
        while True:            
            print('计时: ', round(time.time() - starttime, 0), '秒', end="\r")
            time.sleep(1)
    except KeyboardInterrupt:
        print('结束')
        endtime = time.time()
        print('总共的时间为:', round(endtime - starttime, 2),'secs')
        break
Copy after login

The test results are as follows:

按下回车开始计时,按下 Ctrl + C 停止计时。

开始
计时:  3.0 秒
计时:  5.0 秒
^C结束 6.0 秒
总共的时间为: 6.69 secs
Copy after login

For more Python related technical articles, please visitPython tutorial column to learn!

The above is the detailed content of How to implement timing in python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template