Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2024
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.51 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.  
  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. # Fourth version
  28. def fake_bin_v4(x: str) -> str:
  29.     new_list = []
  30.     for digit in list(x):
  31.         if int(digit) < 5:
  32.             new_list.append('0')
  33.         else:
  34.             new_list.append('1')
  35.     return "".join(new_list)
  36.  
  37.  
  38. # Performance wrapper
  39. def performance_test(func, input_data, iterations=100000):
  40.     start_time = time.time()
  41.     for _ in range(iterations):
  42.         func(input_data)
  43.     end_time = time.time()
  44.     return end_time - start_time
  45.  
  46. # Test data
  47. input_data = "1234567890" * 10  # A longer string to better measure performance
  48.  
  49. # Testing performance
  50. performance_v1 = performance_test(fake_bin_v1, input_data)
  51. performance_v2 = performance_test(fake_bin_v2, input_data)
  52. performance_v3 = performance_test(fake_bin_v3, input_data)
  53. performance_v4 = performance_test(fake_bin_v4, input_data)
  54.  
  55. # Results
  56. print(f"Version 1: {performance_v1:.6f} seconds")
  57. print(f"Version 2: {performance_v2:.6f} seconds")
  58. print(f"Version 3: {performance_v3:.6f} seconds")
  59. print(f"Version 4: {performance_v4:.6f} seconds")
  60.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement