The following is a function calculation code in python:
loops=25000000from math import*
a=range(1,loops)
def f(x):return3*cos(x)+4*sin(x)**2%timeit r=(f(x)for x in a)
effectiveness:
1000000 loops, best of 3: 552 ns per loop
Let's take a look at ways to improve the calculation speed:
1、 Use array
import numpy as np
a = np.arange(1, loops)%timeit r =3* np.cos(a)+ np.sin(a)**2
effectiveness:
1 loop, best of 3: 3.87 s per loop
2、 Use digital expression library numexpr
import numexpr as ne
ne.set_num_threads(1)
f ='3 * log(a) + cos(a) **2'%timeit r = ne.evaluate(f)
effectiveness:
1 loop, best of 3: 2.15 s per loop
3、 Use multithreading
ne.set_num_threads(4)%timeit r = ne.evaluate(f)
effectiveness:
1 loop, best of 3: 1.14 s per loop
So far, this article on how to improve the calculation speed of python is introduced. For more information about how to improve the calculation speed in python, please search for the previous articles of ZaLou.Cn or continue to browse the related articles below. I hope you will support ZaLou more in the future. Cn!
Recommended Posts