This example shows that python calcuation with float numbers is faster than with integer numbers. I am wondering why calculation with integer is not faster than with float
import time
# Number of operations
N = 10**7
# Integer addition
start_time = time.time()
int_result = 0
for i in range(N):
int_result += 1
int_time = time.time() - start_time
# Float addition
start_time = time.time()
float_result = 0.0
for i in range(N):
float_result += 1.0
float_time = time.time() - start_time
print(f"Time taken for integer addition: {int_time:.6f} seconds")
print(f"Time taken for float addition: {float_time:.6f} seconds")
float is a thin wrapper around whatever floating-point type your hardware provides. int is not; it's an arbitrary-precision type based on arrays of large "digits". A "small" integer is still a single-element array of machine words, rather than a single machine word itself. Operations on such values are necessarily slower than a single machine-integer operation would be.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With