简体   繁体   English

有没有办法通过浮点精度或限制浮点范围来加速 python?

[英]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.在 glsl 中,可以使用低精度浮点数来加快计算速度。 Is there a way to limit the precision or range of floats in python to speed up the math?有没有办法限制 python 中浮点数的精度或范围以加快数学运算?

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.您可以使用numba.njit(fastmath=True)并下载 Intel SVML 来加速计算,当然作为回报,它使用不安全的浮点数。 You can check the documentaries here Numba Just-In-Time Compilation您可以在此处查看纪录片Numba 即时编译

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM