fork(1) download
  1. #!/usr/bin/env python3
  2.  
  3. import timeit
  4.  
  5. def f(n=1024):
  6. buf = b""
  7.  
  8. for i in range(20480):
  9. buf += b'c'
  10.  
  11. return buf
  12.  
  13. def g(n=1024):
  14. buf = bytearray()
  15.  
  16. val = b'c'[0]
  17.  
  18. for i in range(20480):
  19. buf.append(val)
  20.  
  21. return bytes(buf)
  22.  
  23. def h(n=1024):
  24. buf = []
  25.  
  26. for i in range(20480):
  27. buf.append(b'c')
  28.  
  29. return b"".join(buf)
  30.  
  31. if __name__ == "__main__":
  32. print(timeit.repeat(f, number=3))
  33. print(timeit.repeat(g, number=3))
  34. print(timeit.repeat(h, number=3))
Success #stdin #stdout 0.32s 9440KB
stdin
Standard input is empty
stdout
[0.07051301002502441, 0.07000398635864258, 0.07023000717163086]
[0.010985136032104492, 0.010872125625610352, 0.010926008224487305]
[0.01329493522644043, 0.013096094131469727, 0.013077974319458008]