'Is there a way to speed up python via float precision or limiting float range?

In glsl one can use low precision floats to speed calculations up. Is there a way to limit the precision or range of floats in python to speed up the math?



Solution 1:[1]

You can use numba.njit(fastmath=True) and download Intel SVML to speed up the calculation, which of course in return, it uses unsafe floating points. You can check the documentaries here Numba Just-In-Time Compilation

from numba import njit

# Without njit(fastmath=True)
def math_multiplication_power_loop(x,y):
    number = []
    
    for i in range(40):
        number.append((x*y)**i)
    
    return sum(number)


# With njit(fastmath=True)
@njit(fastmath=True)
def math_multiplication_power_loop_njit(x,y):
    number = []
    
    for i in range(40):
        number.append((x*y)**i)
    
    return sum(number)

x = 12344
y = 5567.8


%timeit math_multiplication_power_loop(x,y)
13.1 µs ± 189 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

%timeit math_multiplication_power_loop_njit(x,y)
1.29 µs ± 21.7 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1