Guest User

Untitled

a guest
Jun 20th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.51 KB | None | 0 0
  1. ### StringIO
  2.  
  3. ```
  4.  
  5. from io import StringIO
  6.  
  7.  
  8. def meth1(string):
  9. a = []
  10. for i in range(100):
  11. a.append(string)
  12. return ''.join(a)
  13.  
  14. def meth2(string):
  15. a = StringIO()
  16. for i in range(100):
  17. a.write(string)
  18. return a.getvalue()
  19.  
  20.  
  21. if __name__ == '__main__':
  22. from timeit import Timer
  23. string = "This is test string"
  24. print(Timer("meth1(string)", "from __main__ import meth1, string").timeit())
  25. print(Timer("meth2(string)", "from __main__ import meth2, string").timeit())
  26.  
  27. ```
Add Comment
Please, Sign In to add comment