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