Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report