Skip to content

Instantly share code, notes, and snippets.

@andrewwatts
Created March 10, 2012 19:33
Show Gist options
  • Select an option

  • Save andrewwatts/2012630 to your computer and use it in GitHub Desktop.

Select an option

Save andrewwatts/2012630 to your computer and use it in GitHub Desktop.

Revisions

  1. andrewwatts revised this gist Jul 28, 2012. 1 changed file with 26 additions and 17 deletions.
    43 changes: 26 additions & 17 deletions request_time.py
    Original file line number Diff line number Diff line change
    @@ -1,33 +1,42 @@
    #!/usr/bin/env python2.7

    import urllib2
    import time

    _ITERATIONS = 200
    _URL = 'http://localhost/tmp/derp.html'
    _NUMBER = 1000


    start_time = time.time()
    for i in range(0, _ITERATIONS):
    def test_urllib2():
    import urllib2
    try:
    response = urllib2.urlopen('http://localhost/tmp/derp.html')
    response = urllib2.urlopen(_URL)
    except urllib2.HTTPError, e:
    response = e
    response.code
    print 'time with urllib2: {0}'.format(time.time()-start_time)
    return response.read()


    import urllib3
    start_time = time.time()
    http = urllib3.PoolManager()
    for i in range(0, _ITERATIONS):
    response = http.request('GET', 'http://localhost/tmp/derp.html')
    def test_urllib3():
    import urllib3
    http = urllib3.PoolManager()
    response = http.request('GET', _URL)
    response.status
    print 'time with urllib3: {0}'.format(time.time()-start_time)
    return response.data


    import requests
    start_time = time.time()
    for i in range(0, _ITERATIONS):
    response = requests.get('http://localhost/tmp/derp.html')
    def test_requests():
    import requests
    response = requests.get(_URL)
    response.status_code
    print 'time with requests: {0}'.format(time.time()-start_time)
    return response.text


    if __name__ == '__main__':
    from timeit import Timer
    t_urllib2 = Timer("test_urllib2()", "from __main__ import test_urllib2")
    print '{0} urllib2: {1}'.format(_NUMBER, t_urllib2.timeit(number=_NUMBER))
    t_urllib3 = Timer("test_urllib3()", "from __main__ import test_urllib3")
    print '{0} urllib3: {1}'.format(_NUMBER, t_urllib3.timeit(number=_NUMBER))
    t_requests = Timer("test_requests()", "from __main__ import test_requests")
    print '{0} requests: {1}'.format(_NUMBER, t_requests.timeit(number=_NUMBER))

  2. andrewwatts created this gist Mar 10, 2012.
    33 changes: 33 additions & 0 deletions request_time.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    #!/usr/bin/env python2.7

    import urllib2
    import time

    _ITERATIONS = 200


    start_time = time.time()
    for i in range(0, _ITERATIONS):
    try:
    response = urllib2.urlopen('http://localhost/tmp/derp.html')
    except urllib2.HTTPError, e:
    response = e
    response.code
    print 'time with urllib2: {0}'.format(time.time()-start_time)


    import urllib3
    start_time = time.time()
    http = urllib3.PoolManager()
    for i in range(0, _ITERATIONS):
    response = http.request('GET', 'http://localhost/tmp/derp.html')
    response.status
    print 'time with urllib3: {0}'.format(time.time()-start_time)


    import requests
    start_time = time.time()
    for i in range(0, _ITERATIONS):
    response = requests.get('http://localhost/tmp/derp.html')
    response.status_code
    print 'time with requests: {0}'.format(time.time()-start_time)