我正在尝试从 python 上的 os.system('ping') 函数确定 time/latency
Am trying to determent the time/latency from os.system('ping') function on python
import os import time for i in range(20): start = time.time() os.system('ping -n 1 {}'.format('google.com')) end = time.time()
我尝试了这个,但最后 - 开始效率不高,所以我试图找到一个解决方案,了解如何从表扬中获得时间
试试这个:
def TicTocGenerator():
# Generator that returns time differences
ti = 0 # initial time
tf = time.time() # final time
while True:
ti = tf
tf = time.time()
yield tf - ti # returns the time difference
TicToc = TicTocGenerator() # create an instance of the TicTocGen generator
def toc(tempBool=True):
# Prints the time difference yielded by generator instance TicToc
tempTimeInterval = next(TicToc)
if tempBool:
print("Elapsed time: %f seconds.\n" % tempTimeInterval)
def tic():
# Records a time in TicToc, marks the beginning of a time interval
toc(False)
tic()
#your code here
toc()
但如果您需要 ping 统计信息,您可以使用:
from pythonping import ping
PingRes = ping(target=self.ip,timeout=3,count=3, verbose=True)
import os import time for i in range(20): start = time.time() os.system('ping -n 1 {}'.format('google.com')) end = time.time()
我尝试了这个,但最后 - 开始效率不高,所以我试图找到一个解决方案,了解如何从表扬中获得时间
试试这个:
def TicTocGenerator():
# Generator that returns time differences
ti = 0 # initial time
tf = time.time() # final time
while True:
ti = tf
tf = time.time()
yield tf - ti # returns the time difference
TicToc = TicTocGenerator() # create an instance of the TicTocGen generator
def toc(tempBool=True):
# Prints the time difference yielded by generator instance TicToc
tempTimeInterval = next(TicToc)
if tempBool:
print("Elapsed time: %f seconds.\n" % tempTimeInterval)
def tic():
# Records a time in TicToc, marks the beginning of a time interval
toc(False)
tic()
#your code here
toc()
但如果您需要 ping 统计信息,您可以使用:
from pythonping import ping
PingRes = ping(target=self.ip,timeout=3,count=3, verbose=True)