Install self-addressed
# #为什么使用NumPy
File vectorsumcompare.py
#!/usr/bin/env python# -*- coding:utf-8 -*-__author__ = ' Teng ' import Sysfrom datetime import datetimeimport numpy as npdef numpysum (n): a = np.arange (n) **2 b = np.arange (n) **3 c = a+b return cdef pythonsum (n): a = range (n) b = range (n) c = [] for i in range (Len (a)): a[i] = i**2 b[i] = i**3 c.append (A[i]+ b[i]) return Csize = int (Sys.argv[1]) Start = datetime.now () c = pythonsum (size) print " Pythonsum: ", cdelta = dateTime.now () - startprint "The last 2 elements of the sum", c[ -2:]print "Pythonsum elapsed time in microseconds", delta.microsecondsstart = datetime.now () c = numpysum (size) print "Numpysum:", cdelta = DateTime.Now () - startprint "The last 2 elements of the sum", c[-2:]print "Numpysum elapsed time in microseconds", delta.microseconds
Run the above script like Python vectorsumcompare.py 10000
Advantages of NumPy
Simple
Fast speed when the data volume is large
# #NumPy数组对象
Debug method shape Returns the size of an element in a tuple tuple to each dimension of the NumPy array
Arange one-dimensional arrays
in [+]: M = Np.array ([Np.arange (2), Np.arange (2)])
in [+]: M
OUT[16]: Array ([[[0, 1],[0, 1]])
in [+]: M.shape
OUT[17]: (2, 2)
Ndarray is a multidimensional array object:
is divided into two parts of the actual data and the metadata that describes the data
NumPy Foundation (i)