Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2024
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.18 KB | None | 0 0
  1. import time
  2.  
  3. # Original function
  4. def fake_bin_v1(x):
  5.     return ''.join('0' if c < '5' else '1' for c in x)
  6.  
  7. # Second version
  8. def fake_bin_v2(x):
  9.     x = list(x)
  10.     for i in range(0, (len(x))):
  11.         if int(x[i]) < 5:
  12.             x[i] = '0'
  13.         else:
  14.             x[i] = '1'
  15.     return "".join(x)
  16.  
  17. # Third version
  18. def fake_bin_v3(x):
  19.     out = ''
  20.     for char in x:
  21.         if int(char) < 5:
  22.             out += '0'
  23.         else:
  24.             out += '1'
  25.     return out
  26.  
  27. # Performance wrapper
  28. def performance_test(func, input_data, iterations=100000):
  29.     start_time = time.time()
  30.     for _ in range(iterations):
  31.         func(input_data)
  32.     end_time = time.time()
  33.     return end_time - start_time
  34.  
  35. # Test data
  36. input_data = "1234567890" * 10  # A longer string to better measure performance
  37.  
  38. # Testing performance
  39. performance_v1 = performance_test(fake_bin_v1, input_data)
  40. performance_v2 = performance_test(fake_bin_v2, input_data)
  41. performance_v3 = performance_test(fake_bin_v3, input_data)
  42.  
  43. # Results
  44. print(f"Version 1: {performance_v1:.6f} seconds")
  45. print(f"Version 2: {performance_v2:.6f} seconds")
  46. print(f"Version 3: {performance_v3:.6f} seconds")
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement