Skip to content

Instantly share code, notes, and snippets.

@ringsaturn
Last active August 22, 2019 07:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ringsaturn/439752de14c87807195e57aa9bcd4f73 to your computer and use it in GitHub Desktop.
Save ringsaturn/439752de14c87807195e57aa9bcd4f73 to your computer and use it in GitHub Desktop.
"""Benchmark IO cross Python2 and Python3.
Result:
1. Python2 + cString
2. Python3 + six.bytes.io
3. Python2 + six.bytes.io
Log:
Python3 six bytes io
running io benchmark on Python3
for 20000 loops, cost 0.015699999999999992
Python2 six bytes io
running io benchmark on Python2
for 20000 loops, cost 0.030913
Python2 cString IO
running io benchmark on Python2
for 20000 loops, cost 0.01276
"""
from __future__ import print_function
import sys
import time
import six
if six.PY3:
timer_func = time.process_time
else:
timer_func = time.clock
if six.PY3:
from six import BytesIO as IO
else:
from cStringIO import StringIO as IO
# from six import BytesIO as IO
io_func = IO
io_obj = io_func
def benchmark():
print("running io benchmark on Python{}".format(str(sys.version[0])))
n = 20000
start = timer_func()
for i in range(n):
io_func(bytes(i))
end = timer_func()
print("for {} loops, cost {}".format(str(n), str(end-start)))
benchmark()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment