Sorry, this page is locked for editing due to heavy traffic and edit volume.
Why use numpy Array instead of Python Lists
This is a part of Python Knowledge and Resources List
Please read the following reddit comment before reading the article
This article is very subjective. All that has been proven by it is that the used functions perform better on
numpy.Array than on python
list when using integers.
However, it completely avoids talking on the fact that:
listare made for heterogeneous types while
numpy.Arrayworks on homogeneous types.
listadding and removing elements where
- The used functions are not described anywhere therefore we cannot assume that the algorithm is the same
listtakes more time than the
meanwhich is suppose to take more time. And
meantakes double the time of
- The protocol only runs the function once, where it should run the function at least 100+ times and get an average
datetime import datetime
nitems = 100000
narray = arange(nitems)
larray = range(nitems)
start = datetime.now()
val = eval(expression)
end = datetime.now()
return "%d micro seconds %s" %((end-start).microseconds,expression)
numpy array sum vs list sum
numpy_op1 = "narray.sum()"
list_op1 = "sum(larray)"
222 micro seconds narray.sum()
931 micro seconds sum(larray)
numpy array min vs list min
numpy_op2 = "narray.min()"
list_op2 = "sorted(larray)"
306 micro seconds narray.min()3691 micro seconds sorted(larray)
numpy array mean vs list average
numpy_op3 = "narray.mean()"
446 micro seconds narray.mean()916 micro seconds sum(larray)/len(larray)
numpy array max vs list max
numpy_op4 = "narray.max()"
list_op4 = "sorted(larray,reverse=True)"
280 micro seconds narray.max()2777 micro seconds sorted(larray,reverse=True)
Please don't ever sort a list to find the maximum. That just makes my head hurt. It's O(n lg n) rather than O(n). Use the min() and max() builtins - it's what they're there for!
Some much needed critique.http://www.reddit.com/r/Python/comments/32flst/why_use_numpy_array_instead_of_python_lists/