fork download
  1. #!/usr/bin/env python3
  2.  
  3. import timeit
  4.  
  5. def f():
  6. buf = b""
  7.  
  8. for i in range(102400):
  9. buf += b'c'
  10.  
  11. return buf
  12.  
  13. def g():
  14. buf = bytearray()
  15.  
  16. for i in range(102400):
  17. buf.append(b'c'[0])
  18.  
  19. return bytes(buf)
  20.  
  21. if __name__ == "__main__":
  22. print(timeit.repeat(f, number=2))
  23. print(timeit.repeat(g, number=2))
Success #stdin #stdout 4s 10296KB
stdin
Standard input is empty
stdout
[1.268664836883545, 1.27207612991333, 1.2689130306243896]
[0.03531384468078613, 0.03574490547180176, 0.03640103340148926]