Skip to content

Instantly share code, notes, and snippets.

@avneesh91
Created January 12, 2020 13:10
Show Gist options
  • Save avneesh91/ff08c43a3b784b30db8791a2b44a5079 to your computer and use it in GitHub Desktop.
Save avneesh91/ff08c43a3b784b30db8791a2b44a5079 to your computer and use it in GitHub Desktop.
Performance Comparision
import time
import requests
from concurrent.futures import ThreadPoolExecutor
url_list = ['https://docs.python.org/3/library/concurrent.futures.html',\
'https://technokeeda.com',
'http://home.pipeline.com/~hbaker1/Futures.html']
def get_sequential_get(urls):
"""
Function which does a get requests
one after the other
"""
result = []
for url in urls:
response = requests.get(url)
result.append([response, response.status_code])
return result
def get_futures_get(urls):
results = []
currs = ThreadPoolExecutor(max_workers=5)
for url in urls:
currs.submit(worker_func, url, results)
currs.shutdown(wait=True)
return results
def worker_func(url, result_list):
response = requests.get(url)
result_list.append([response, response.status_code])
def calculate_function_time(curr_func, **kwargs):
start = time.localtime()
curr_func(**kwargs)
end = time.localtime()
return end.tm_sec - start.tm_sec
if __name__ == '__main__':
print('Time taken by normal implementation {} seconds'.format(calculate_function_time(get_sequential_get, **{'urls': url_list})))
print('Time taken by futures implementation {} seconds'.format(calculate_function_time(get_futures_get, **{'urls': url_list})))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment