AssignmentGuru

Reddit: rccnls27: JohnnyJordaan: elapsed time

Jun 21st, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.45 KB | None | 0 0
  1. # http://python.assignmentsolutionguru.com/
  2.  
  3. import datetime
  4. rand_word = "brunette"
  5. count = 10000
  6. count2 = 100
  7.  
  8. def with_length():
  9.     for i in range(count):
  10.         result_str = list("_" * len(rand_word))
  11.  
  12. def without_length():
  13.     for i in range(count):
  14.         result_str = [ '_' for c in rand_word ]
  15.  
  16. if __name__ == '__main__':
  17.    
  18.     sum_with = 0
  19.     for i in range(count2):
  20.         start_with_length = datetime.datetime.now()
  21.         with_length()
  22.         end_with_length = datetime.datetime.now()
  23.         sum_with += (end_with_length - start_with_length).microseconds
  24.  
  25.     sum_without = 0
  26.     for i in range(count2):
  27.         start_without_length = datetime.datetime.now()
  28.         without_length()
  29.         end_without_length = datetime.datetime.now()
  30.         sum_without += (end_without_length - start_without_length).microseconds
  31.        
  32.     print("with_length() took microseconds (average):", sum_with / count2)
  33.     print("without_length() took microseconds (average):", sum_without / count2)
  34.    
  35.  
  36. ### OUTPUT ###
  37. # RUN 1 #
  38. # with_length() took microseconds (average): 25097.74
  39. # without_length() took microseconds (average): 33223.47
  40. #
  41. # RUN 2 #
  42. # with_length() took microseconds (average): 22505.94
  43. # without_length() took microseconds (average): 30781.8
  44. #
  45. # RUN 3 #
  46. # with_length() took microseconds (average): 27909.81
  47. # without_length() took microseconds (average): 35745.27
  48.  
  49. # http://python.assignmentsolutionguru.com/
Add Comment
Please, Sign In to add comment