Advertisement
RedstoneHair

string mutation methods, fast and slow

Nov 9th, 2022
1,094
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.36 KB | None | 0 0
  1. import time
  2.  
  3. text = "Hello"
  4.  
  5. before = time.time()
  6. for _ in range(100000000):
  7.     text += '0'
  8. print(f'slow method: {time.time()-before:.02f}')
  9.  
  10.  
  11. text = ["Hello"]
  12.  
  13. before = time.time()
  14. for _ in range(100000000):
  15.     text.append('0')
  16. final = ''.join(text)
  17. print(f'slow method: {time.time()-before:.02f}')
  18.  
  19. # slow method: 645.54
  20. # slow method: 19.04
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement