1 回答

TA貢獻(xiàn)1854條經(jīng)驗(yàn) 獲得超8個(gè)贊
那是因?yàn)?GNUtime使用默認(rèn)格式字符串,更詳細(xì),但您需要-p選項(xiàng)。
引用手冊(cè):
默認(rèn)格式字符串是:
%Uuser %Ssystem %Eelapsed %PCPU (%Xtext+%Ddata %Mmax)k %Iinputs+%Ooutputs (%Fmajor+%Rminor)pagefaults %Wswaps
當(dāng)給出 -p 選項(xiàng)時(shí),使用(便攜式)輸出格式:
real %e
user %U
sys %S
您還需要對(duì)輸出進(jìn)行解碼,否則您將得到bytes而不是str,并且不會(huì)解釋換行符。前任:
>>> print(b'hello\nworld\n')
b'hello\nworld\n'
>>> print('hello\nworld\n')
hello
world
所以我會(huì)按原樣修復(fù)您的代碼:
command = ['time', '-p', programs[0], files[0]]
out = subprocess.check_output(command, stderr=subprocess.STDOUT)
print(out.decode())
編輯:另一個(gè)答案似乎有助于通過(guò)使用內(nèi)置的 shell 來(lái)修復(fù)丟失的小數(shù)。您可以混合兩個(gè)答案以獲得所需的字符串輸出,并帶有足夠的小數(shù)。
請(qǐng)注意,除非您想對(duì)命令使用分析器,否則您似乎無(wú)法做得更好(請(qǐng)參閱如何獲取 Python 程序的執(zhí)行時(shí)間?)
添加回答
舉報(bào)