如何获得 Python 程序的执行时间? [英] How do I get time of a Python program's execution?

查看:78
本文介绍了如何获得 Python 程序的执行时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Python 命令行程序,需要一段时间才能完成.我想知道完成运行所需的确切时间.

I have a command line program in Python that takes a while to finish. I want to know the exact time it takes to finish running.

我查看了 timeit 模块,但它似乎只适用于小代码片段.我想为整个程序计时.

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

推荐答案

在 Linux 或 Unix 中:

In Linux or Unix:

$ time python yourprogram.py

在 Windows 中,请参阅此 StackOverflow 问题:如何在 Windows 命令行上测量命令的执行时间?

In Windows, see this StackOverflow question: How do I measure execution time of a command on the Windows command line?

对于更详细的输出,

$ time -v python yourprogram.py
    Command being timed: "python3 yourprogram.py"
    User time (seconds): 0.08
    System time (seconds): 0.02
    Percent of CPU this job got: 98%
    Elapsed (wall clock) time (h:mm:ss or m:ss): 0:00.10
    Average shared text size (kbytes): 0
    Average unshared data size (kbytes): 0
    Average stack size (kbytes): 0
    Average total size (kbytes): 0
    Maximum resident set size (kbytes): 9480
    Average resident set size (kbytes): 0
    Major (requiring I/O) page faults: 0
    Minor (reclaiming a frame) page faults: 1114
    Voluntary context switches: 0
    Involuntary context switches: 22
    Swaps: 0
    File system inputs: 0
    File system outputs: 0
    Socket messages sent: 0
    Socket messages received: 0
    Signals delivered: 0
    Page size (bytes): 4096
    Exit status: 0

这篇关于如何获得 Python 程序的执行时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆