Advertisement
Guest User

Python Benchmarking

a guest
Nov 22nd, 2020
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. import time
  2.  
  3.  
  4.  
  5. def solveoriginal(s):
  6. ls = s.split(" ")
  7. new_word = ""
  8. new_name = ""
  9. for word in ls:
  10. if len(word) > 1:
  11. new_word = new_word + word[0].title() + word[1:] + " "
  12. else:
  13. new_word = new_word + word.title() + " "
  14. new_name = new_name.join(new_word)
  15. return new_name
  16.  
  17.  
  18. def cap_first_letter(word):
  19. return word[:1].upper() + word[1:]
  20.  
  21.  
  22. def solvenew(words):
  23. return ' '.join(cap_first_letter(word) for word in words.split(' '))
  24.  
  25.  
  26.  
  27.  
  28. names = "ALLISION heck sanders"
  29. for iterations in (10 ** 6, 11 ** 6, 10 ** 7):
  30. print(f"\n{iterations} iteartions\n")
  31.  
  32.  
  33. start = time.time()
  34. for _ in range(iterations): solvenew(names)
  35. print(f"Time taken for new function: {time.time() - start:.3f} s")
  36.  
  37. start = time.time()
  38. for _ in range(iterations): solveoriginal(names)
  39. print(f"Time taken for original function: {time.time() - start:.3f} s")
  40.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement