我想知道程序运行的时间,所以我尝试了"/usr/bin/time ./program > /dev/null"。但很快我发现它将程序的输出显示到标准错误输出(stderr)。然后我尝试了"/usr/bin/time ./program > /dev/null 2>&1",但/usr/bin/time的输出没有出现。所以我的问题是,如何忽略程序的输出并保留time命令的输出。
非常感谢。
非常感谢。
使用-o
标志将时间输出发送到除stderr
之外的其他地方。例如,如果您仍希望在屏幕上看到它:
/usr/bin/time -o /dev/tty ./program >/dev/null 2>&1
或者,如果你非常需要在stdout
上输出:
/usr/bin/time sh -c './program >/dev/null 2>&1'
或类似的。然而,现在您还测量外壳启动进程所需的时间,这可能是一个问题,也可能不是。
(无法评论)
使用子shell可能更好:
/usr/bin/time ( ./program > /dev/null 2>&1 ) 2>&1
或者更好的是一个复合结构:
/usr/bin/time { ./program > /dev/null 2>&1; } 2>&1