我试图测量通过子进程调用的可执行程序的执行时间(以秒为单位).我不希望发出可执行文件的输出(stderr或stdout).
我已经尝试了timeit和资源库,但是没有准确地捕获进程的时间,看起来它只捕获Python工作线程中的时间.
以下尝试将丢失stderr重定向的时间信息b/c.但是,如果没有stderr重定向,将发出命令'f_cmd'stderr输出.
def doWithTiming(f_cmd):
DEVNULL = open(os.devnull, 'w')
return subprocess.check_output([ "/usr/bin/time", "--format=%e seconds"] + f_cmd.split(), stderr=DEVNULL)
Run Code Online (Sandbox Code Playgroud)
如何忽略f_cmd的所有输出但保留/ usr/bin/time的输出?