我正在编写一个将在命令行中运行的 Python 脚本。这个想法是从用户那里获取命令,运行它,然后提供用户提供的命令的挂钟时间和 CPU 时间。请参阅下面的代码。
#!/usr/bin/env python
import os
import sys
def execut_cmd(cmd_line):
utime = os.system('time '+cmd_line)
# Here I would like to store the wall-clock time in the Python variable
# utime.
cputime = os.system('time '+cmd_line)
# Here the CPU time in the cputime variable. utime and cputime are going to
# be used later in my Python script. In addition, I would like to silence the
# output of time in the screen.
execut_cmd(sys.argv[1])
print ('Your command wall-clock time is '+utime)
print ('Your command cpu time is '+ cputime)
我怎样才能做到这一点?此外,如果有比使用“时间”更好的方法,我愿意尝试。