本文使用python中range()函数,可参考子篇文章http://blog.csdn.net/a1212125/article/details/77895811
NumPy系统是Python的一种开源的数值计算扩展。这种工具可用来存储和处理大型矩阵,比Python自身的嵌套列表(nested list structure)结构要高效的多(该结构也可以用来表示矩阵(matrix))。
我们做一个简单的向量加法,其中a和b是两个向量,其每一个元素分别是从零开始的平方数1,4,9,16…..和立方数1,8,27……,我们来指定向量的元素的个数.
以下代码使用python2.7,构造两个函数分别是sumvactor_python(n),sumvactor_numpy(n)
实现了同样的功能并输出比较所用的时间.
import sys
from datetime import datetime
import numpy
def sumvactor_python(n):
"""with python2.7 code,make a and b for summary """
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 c
def sumvactor_numpy(n):
"""with function arange() in numpy ,
make a and b for summary """
a=numpy.arange(n)**2
b=numpy.arange(n)**3
c=a+b
return c
size=int(sys.argv[1])
start=datetime.now()
c=sumvactor_python(size)
time=datetime.now() - start
print("the last 2 element is",c[-2:])
print("python elapsed time in microsecondes",time.microseconds)
start=datetime.now()
c=sumvactor_numpy(size)
time=datetime.now() - start
print("the last 2 element is",c[-2:])
print("python elapsed time in microsecondes",time.microseconds)
程序输出a,b向量最后的两个元素,同时输出a,b相加所用的时间.
- 我们给出元素个数为1000,输出:
('the last 2 element is', [995007996, 998001000])
('python elapsed time in microsecondes', 469)
('the last 2 element is', array([995007996, 998001000]))
('python elapsed time in microsecondes', 202)
时间上纯python用时是numpy的两倍.
2. 我们给出更高的元素个数5000,输出:
('the last 2 element is', [124875039996, 124950005000])
('python elapsed time in microsecondes', 2652)
('the last 2 element is', array([124875039996, 124950005000]))
('python elapsed time in microsecondes', 894)
时间上纯python用时是numpy的3倍更多.
所以我们直观上就能看出,庞大的数据的处理中,numpy在速度上越是有其优势.