[Python] Why is python calculation with float numbers faster than calculation with integer...

Discussão em 'Python' iniciado por Stack, Outubro 2, 2024.

  1. Stack

    Stack Membro Participativo

    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")

    Continue reading...

Compartilhe esta Página