Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2018
577
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.47 KB | None | 0 0
  1. import timeit
  2.  
  3. def wrapper(func, *args, **kwargs):
  4.  
  5.     def wrapped():
  6.         return func(*args, **kwargs)
  7.  
  8.     return wrapped
  9.  
  10. def is_compare(comparison_count):
  11.     total_count = 0
  12.     for _ in range(comparison_count):
  13.         if 1 is -1:
  14.             # should never get here
  15.             total_count = 0
  16.         else:
  17.             total_count += 1
  18.     assert total_count == comparison_count, "Error: *is* test did not run correctly."
  19.  
  20. def eq_compare(comparison_count):
  21.     total_count = 0
  22.     for _ in range(comparison_count):
  23.         if 1 == -1:
  24.             # should never get here
  25.             total_count = 0
  26.         else:
  27.             total_count += 1
  28.     assert total_count == comparison_count, "Error: *==* test did not run correctly."
  29.  
  30.  
  31. class TestIsVsEq(object):
  32.     is_wins = 0
  33.     is_diff = []  # track difference between comparison times
  34.     eq_wins = 0
  35.     eq_diff = []  # track difference between comparison times
  36.  
  37.     def __init__(self, is_function, eq_function, **kwargs):
  38.         self.is_function = is_function
  39.         self.eq_function = eq_function
  40.         self.times = kwargs.get("times", 100)
  41.         self.verbose = kwargs.get("verbose", False)
  42.  
  43.     @staticmethod
  44.     def verify_python_standards():
  45.         assert -1 is -1, "This test won't work because this version of Python doesn't follow the unwritten rules."
  46.  
  47.     def show_stats(self):
  48.         assert self.is_wins + self.eq_wins != 0, "No tests run!"
  49.  
  50.         is_won_competition = self.is_wins > self.eq_wins
  51.         if is_won_competition:
  52.             average = sum(self.is_diff) / len(self.is_diff)
  53.         else:
  54.             average = sum(self.eq_diff) / len(self.eq_diff)
  55.  
  56.         print("Out of {} tests, *{}* won {} times!".format(self.is_wins + self.eq_wins,
  57.                                                            'is' if is_won_competition else 'eq',
  58.                                                            max(self.is_wins, self.eq_wins)))
  59.         print("*{}* won {} times.".format('eq' if is_won_competition else 'is',
  60.                                           min(self.is_wins, self.eq_wins)))
  61.  
  62.         print("Average difference in time was {} seconds.".format(average))
  63.  
  64.     def run_test(self):
  65.         print("Testing *is* comparisons {} times.".format(self.times))
  66.         is_time = timeit.timeit(self.is_function, number=self.times)
  67.         print("Testing *==* comparisons {} times.".format(self.times))
  68.         eq_time = timeit.timeit(self.eq_function, number=self.times)
  69.  
  70.         is_won_round = is_time < eq_time
  71.  
  72.         if is_won_round:
  73.             self.is_wins += 1
  74.             self.is_diff.append(eq_time - is_time)
  75.         else:
  76.             self.eq_wins += 1
  77.             self.eq_diff.append(is_time - eq_time)
  78.  
  79.         print("*{}* is the faster operator!".format('is' if is_won_round else '=='))
  80.         print("\tis_time: {}".format(is_time))
  81.         print("\teq_time: {}".format(eq_time))
  82.  
  83. if __name__ == '__main__':
  84.     number_of_comparisons = 2 ** 20
  85.  
  86.     test = TestIsVsEq(is_function = wrapper(is_compare, number_of_comparisons),
  87.                       eq_function = wrapper(eq_compare, number_of_comparisons),
  88.                       verbose=True)
  89.  
  90.     test.verify_python_standards()
  91.  
  92.     while True:
  93.         try:
  94.             test.run_test()
  95.             if test.verbose and len(test.is_diff + test.eq_diff) > 0:
  96.                 test.show_stats()
  97.  
  98.         except KeyboardInterrupt:
  99.             print("")
  100.             test.show_stats()
  101.             exit(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement