相关疑难解决方法(0)

使用/ usr/bin/time的Python子进程:如何捕获时序信息但忽略所有其他输出?

我试图测量通过子进程调用的可执行程序的执行时间(以秒为单位).我不希望发出可执行文件的输出(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的输出?

python time stdout stderr python-2.7

2
推荐指数
1
解决办法
4288
查看次数

标签 统计

python ×1

python-2.7 ×1

stderr ×1

stdout ×1

time ×1