Get the App
SLTechnology News&Howtos  ›  Development  › 

How to compare the operation speed between Python built-in function and numPy

Shulou Source: shulou.com Published: 2022-06-03 05:50:12 09月30日 Update

This article shows you how to compare the operation speed of Python built-in functions and numPy. The content is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Python brings several functions of its own, mainly sum,max,min, and there are also several similar functions in numPy. Today, after comparing the operation speed of several functions, we found that the array calculation speed of numpy is the fastest.

The train of thought is that by generating 10,000 random numbers, summing them with four methods, and finding the maximum value, the way of finding the mean value is the same as the summation, and the way of finding the minimum value is also similar to the maximum value, so only the summation and the maximum value are measured.

Import randomimport timeimport numpy as npfrom pandas import Seriesa= [] for i in range (100000000): a.append (random.random ()) t1=time.time () sum1=sum (a) # directly use built-in functions to find t2=time.time () sum2=np.sum (a) # directly use numpy to find t3=time.time () b=np.array (a) t4=time.time () sum3=np.sum (b) # convert numpy to array and then find t5=time.time () c=Series (a) t6=time.time () sum4=c.sum () # use pandas Ask t7=time.time () print t2-t1 for the Series object of T3Meit2Gen T5PUBE T4MIT T7MAIT6

The final results are # sum 1.60611581802 9.87746500969 0.223296165466 1.66015696526

It can be seen that the numpy calculation method with array as the object is the fastest, while the direct calculation with numpy is the slowest, and the speed of built-in function ranks second.

Seek the maximum value

Import randomimport timeimport numpy as npfrom pandas import Seriesa= [] for i in range (100000000): a.append (random.random ()) t1=time.time () sum1=max (a) # directly use built-in functions to find t2=time.time () sum2=np.max (a) # directly use numpy to find t3=time.time () b=np.array (a) t4=time.time () sum3=np.max (b) # convert numpy to array and then find t5=time.time () c=Series (a) t6=time.time () sum4=c.max () # use pandas Ask t7=time.time () print t2-t1 for Series object T3Meit2Gen T5PUBE T4MIT T7MAIT6

The result is:

# max 2.81509399414 9.83987283707 0.219717025757 1.62969207764

As a result, numpy with array as the calculation object is still the fastest.

To sum up, if the operation speed is taken into account, the object should be converted to array first, and then calculated with numpy, which can get the fastest calculation speed.

The above is how to compare the speed of Python built-in functions with that of numPy. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Tags: Function speed object operation maximum fastest maximum way result content skill knowledge minimum same concise concise at the same time mean that is train of thought Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS OPPO Reno Shulou Technology NVidia Shulou Information