【问题标题】:Capture run time of python script executed inside shell script [duplicate]捕获在 shell 脚本中执行的 python 脚本的运行时间 [重复]
【发布时间】:2017-09-11 00:18:18
【问题描述】:

我有 bash shell 脚本,它在内部调用 python 脚本。我想知道执行 python 需要多长时间。我不允许在 python 脚本中进行更改。 任何线索都会有所帮助,提前致谢。

【问题讨论】:

    标签: python bash shell


    【解决方案1】:

    可以使用time命令获取python脚本的运行时间。

     ]$ cat time_test.bash
     #!/bin/bash
    
     # You can use:  time python script.py 
     time python -c 'import os;os.getenv("HOME")'
    

    输出会是这样的

     ]$ ./time_test.bash 
    
    real    0m0.010s
    user    0m0.005s
    sys 0m0.005s
    

    【讨论】:

    • 我会尝试这段代码,只是想知道。有没有办法可以将执行时间值存储在变量中?
    【解决方案2】:

    使用/usr/bin/time script 调用python 脚本。这允许您跟踪脚本的 CPU 和挂钟时间。

    【讨论】:

    • 这将为执行 bash 脚本而不是 python 脚本提供时间。
    • 是的,它会告诉我们shell脚本而不是内部调用的python脚本所花费的时间。
    猜你喜欢
    • 2019-02-07
    • 1970-01-01
    • 2018-02-19
    • 1970-01-01
    • 2022-11-16
    • 2011-05-21
    • 1970-01-01
    • 1970-01-01
    • 2015-04-17
    相关资源
    最近更新 更多