Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2024
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.84 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. # Performance wrapper
  18. def performance_test(func, input_data, iterations=100000):
  19.     start_time = time.time()
  20.     for _ in range(iterations):
  21.         func(input_data)
  22.     end_time = time.time()
  23.     return end_time - start_time
  24.  
  25. # Test data
  26. input_data = "1234567890" * 10  
  27.  
  28. # Testing performance
  29. performance_v1 = performance_test(fake_bin_v1, input_data)
  30. performance_v2 = performance_test(fake_bin_v2, input_data)
  31.  
  32. # Results
  33. print(f"Version 1: {performance_v1:.6f} seconds")
  34. print(f"Version 2: {performance_v2:.6f} seconds")
  35.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement