From 44022fe895aebbb2a713d7a5031bfd0d362e357c Mon Sep 17 00:00:00 2001 From: Matt Martz Date: Fri, 23 Aug 2013 09:31:45 -0500 Subject: [PATCH] Improve performance when determining the best server. See #31 and #29. --- speedtest_cli.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/speedtest_cli.py b/speedtest_cli.py index 3defdd7..dfecb84 100755 --- a/speedtest_cli.py +++ b/speedtest_cli.py @@ -316,19 +316,19 @@ def getBestServer(servers): results = {} for server in servers: - cum = 0 + cum = [] url = os.path.dirname(server['url']) for i in range(0, 3): uh = urlopen('%s/latency.txt' % url) start = time.time() - text = uh.read().strip() + text = uh.read(9).strip() total = time.time() - start if int(uh.code) == 200 and text == 'test=test'.encode(): - cum += total + cum.append(total) else: - cum += 3600 + cum.append(3600) uh.close() - avg = round((cum / 3) * 1000000, 3) + avg = round((sum(cum) / 3) * 1000000, 3) results[avg] = server fastest = sorted(results.keys())[0]