Advertisement
DigitalMag

Python str benchmark

Dec 24th, 2019
437
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.61 KB | None | 0 0
  1. import time
  2.  
  3. def test0(n):
  4.     for i in range(n):
  5.         line = bytearray()
  6.         line.extend(('1','2','3'))
  7.  
  8. def test1(n):
  9.     for i in range(n):
  10.         line = '{0}{1}{2}'.format('1','2','3')
  11.  
  12. def test2(n):
  13.     for i in range(n):
  14.         line = '%s%s%s'%('1','2','3')
  15.  
  16.  
  17. def test3(n):
  18.     for i in range(n):
  19.         line = ''.join(('1','2','3'))
  20.  
  21. def test4(n):
  22.     for i in range(n):
  23.         line = '1'+'2'+'3'
  24.  
  25.  
  26.  
  27. if __name__ == '__main__':
  28.  
  29.     a =[test0, test1, test2, test3, test4]
  30.     for m in a:
  31.  
  32.         s = time.time()
  33.         m(100000)
  34.  
  35.         e = time.time()
  36.         print(e-s)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement