Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import time
- # Original function
- def fake_bin_v1(x):
- return ''.join('0' if c < '5' else '1' for c in x)
- # Second version
- def fake_bin_v2(x):
- x = list(x)
- for i in range(0, (len(x))):
- if int(x[i]) < 5:
- x[i] = '0'
- else:
- x[i] = '1'
- return "".join(x)
- # Third version
- def fake_bin_v3(x):
- out = ''
- for char in x:
- if int(char) < 5:
- out += '0'
- else:
- out += '1'
- return out
- # Performance wrapper
- def performance_test(func, input_data, iterations=100000):
- start_time = time.time()
- for _ in range(iterations):
- func(input_data)
- end_time = time.time()
- return end_time - start_time
- # Test data
- input_data = "1234567890" * 10 # A longer string to better measure performance
- # Testing performance
- performance_v1 = performance_test(fake_bin_v1, input_data)
- performance_v2 = performance_test(fake_bin_v2, input_data)
- performance_v3 = performance_test(fake_bin_v3, input_data)
- # Results
- print(f"Version 1: {performance_v1:.6f} seconds")
- print(f"Version 2: {performance_v2:.6f} seconds")
- print(f"Version 3: {performance_v3:.6f} seconds")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement