How does python improve the calculation speed

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

How does python improve the calculation speed
How does python change the environment
How does Python handle the json module
How does Python output integers
How does python output backslashes
How does python call the key of a dictionary
How does python get input examples from the keyboard
How does python update packages
How does python handle the program cannot be opened
How does python judge the module installation is complete
How does python perform matrix operations
How does Python list update value
How Python implements the mail function
How to save the python program
How does python call java classes
How does python import dependency packages
How does python enter interactive mode
How does Python generate xml files
How Python implements the timer function
How to view the python module
How does python determine prime numbers
How does python call its own function
How to learn the Python time module
How does python distinguish return and yield
How does Python call cmd using OS modules
How to use the round function in python
How to use the zip function in Python
What does the tab key in python mean
How to use the format function in python
How to enter python through the command line
How about learning python at the age of 27?
2.1 The Python Interpreter (python interpreter)
How to switch the hosts file using python
How to install the downloaded module in python
Python how to move the code collectively right
Python is short-what does the spirit cage say
How does Python store data to json files
How to perform continuous multiplication calculation in python
How to practice after the python file is written
How to understand the introduction of packages in Python
Consolidate the Python foundation (2)
How python was invented
How Python parses XML
How long does it take to learn python by myself?
How to find the area of a circle in python
How to improve the speed of Linux installation software? Configure DNF/YUM domestic download source for CentOS8