Skip to content

Instantly share code, notes, and snippets.

@amyangfei
Last active August 29, 2015 14:10
Show Gist options
  • Save amyangfei/67851de34e20166f98eb to your computer and use it in GitHub Desktop.
Save amyangfei/67851de34e20166f98eb to your computer and use it in GitHub Desktop.
import io, cStringIO, StringIO
import time
import functools
from memory_profiler import memory_usage
def data_gen():
return 'datadata' * 100
times = 1000000
dflt_interval = 0.5
def print_timing(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
t1 = time.time()
res = func(*args, **kwargs)
t2 = time.time()
print '{} took {:0.3f} ms'.format(func.func_name, (t2-t1)*1000.0)
return res
return wrapper
def test_wrapper(number):
def actual_decorator(func):
@functools.wraps(func)
def returned_func(*args, **kwargs):
for i in range(number):
func(*args, **kwargs)
return returned_func
return actual_decorator
@print_timing
@test_wrapper(times)
def test_cStringIO():
s = cStringIO.StringIO(data_gen())
s.read()
s = cStringIO.StringIO()
s.write(data_gen())
s.seek(0)
@print_timing
@test_wrapper(times)
def test_io_BytesIO():
s = io.BytesIO(data_gen())
s.read()
s = io.BytesIO()
s.write(data_gen())
s.seek(0)
@print_timing
@test_wrapper(times)
def test_StringIO():
s = StringIO.StringIO(data_gen())
s.read()
s = StringIO.StringIO()
s.write(data_gen())
s.seek(0)
print memory_usage((test_cStringIO, (), {}), interval=dflt_interval)
print memory_usage((test_io_BytesIO, (), {}), interval=dflt_interval)
print memory_usage((test_StringIO, (), {}), interval=dflt_interval)
➜ python bytesio_test.py
test_cStringIO took 2297.567 ms
[9.3046875, 9.5078125, 40.39453125, 40.39453125, 40.39453125, 40.39453125, 32.76171875]
test_io_BytesIO took 2722.704 ms
[32.796875, 32.96875, 40.42578125, 40.42578125, 40.42578125, 40.42578125, 40.42578125, 40.42578125]
test_StringIO took 6708.684 ms
[40.44140625, 40.44140625, 48.0703125, 48.0703125, 48.0703125, 48.0703125, 48.0703125, 48.0703125, 48.0703125, 48.0703125, 48.0703125, 48.0703125, 48.0703125, 48.0703125, 48.0703125, 48.0703125]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment