fork(2) download
  1. import io
  2. import timeit
  3. from itertools import chain, groupby
  4.  
  5. chunk_sizes = [5, 11, 1024]
  6.  
  7. x = (b'x'*25+b'\n')*1000
  8. inputs = [[x[i:i+chunk_size] for i in range(0, len(x), chunk_size)]
  9. for chunk_size in chunk_sizes]
  10.  
  11. def make_strings(G):
  12. strings = chain.from_iterable(map(bytes.decode, G))
  13. for k, g in groupby(strings, key=lambda c: c not in '\n\r'):
  14. if k:
  15. yield ''.join(g)
  16.  
  17. class ReadableIterator(io.IOBase):
  18. def __init__(self, it):
  19. self.it = iter(it)
  20. def read(self, n):
  21. # ignore argument, nobody actually cares
  22. # note that it is *critical* that we suppress the `StopIteration` here
  23. return next(self.it, b'')
  24. def readable(self):
  25. return True
  26.  
  27. for chunk_size, input_chunks in zip(chunk_sizes, inputs):
  28. print(chunk_size)
  29. print(timeit.timeit('list(io.TextIOWrapper(ReadableIterator(iter(input_chunks)), newline=""))',
  30. number=10, globals=globals()))
  31. print(timeit.timeit('list(make_strings(iter(input_chunks)))',
  32. number=10, globals=globals()))
Success #stdin #stdout 0.28s 9812KB
stdin
Standard input is empty
stdout
5
0.07765865325927734
0.04711099714040756
11
0.04073921963572502
0.04300684109330177
1024
0.0021248050034046173
0.03758919984102249