我用子进程调用了ping命令。我计算的时差低于运行时间。
延迟:361.953 [实际操作时间]
calculated_latency:0.7531721591949463 [用python计算]
如果python逐行执行,那么计算的延迟将高于原始延迟。我不知道发生了什么,我怎么能计算函数调用的时间。
import subprocess
import shlex
import re
import time
from datetime import datetime
command_line = "ping -c 1 {}".format('yahoo.com')
args = shlex.split(command_line)
try:
start = time.time()
output = subprocess.check_output(args)
end = time.time()
ping_latency_calculated = end - start
result = str(output).strip("b'")
m = re.search(r"\/(\d+\.\d+)\/", result)
ping_latency = m.group(1)
result = {'latency': ping_latency, 'calculated_latency': ping_latency_calculated}
except subprocess.CalledProcessError as e:
result = {'Status': 'Ping Failed', 'time': datetime.fromtimestamp(time.time())}
return result
答案 0 :(得分:2)
calculated_latency
以秒计算,latency
以毫秒计算。
所以你得到的结果是:
latency: 361.953 ms
calculated_latency: 753.1721591949463 ms