- 你的赞助是我们前进的动力:
一流企业专家自动化性能接口测试 数据分析 python一对一教,非骗人的培训机构(多数大陆培训机构的老师实际未入门)承接excel合并,电脑自动化操作等工程 并欢迎讨论中医草药风水相学等道家国学
qq群python 测试开发自动化测试 144081101 教你做免费的线上博客(放在简历中增加亮点),自动化测试平台,性能测试工具等,让你有实际项目经验 联系qq:37391319
交流QQ群:python 测试开发自动化测试 144081101 Python数据分析pandas Excel 630011153 中医草药自学自救大数据 391441566 南方中医草药鉴别学习 184175668 中医草药湿热湿疹胃病 291184506 python高级人工智能视觉 6089740
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | #!/usr/bin/env/ python3
# -*- coding: utf-8 -*-
# Author: china-testing#126.com 技术支持qq群:6089740
# CreateDate: 2018-04-12
# vectorsum.py
import sys
from datetime import datetime
import numpy as np
"""
该段代码演示Python中的向量加法
使用如下命令运行程序:
python vectorsum.py n
n为指定向量大小的整数
加法中的第一个向量包含0到n的整数的平方
第二个向量包含0到n的整数的立方
程序将打印出向量加和后的最后两个元素以及运行消耗的时间
"""
def numpysum(n):
a = np.arange(n) ** 2
b = np.arange(n) ** 3
c = a + b
return c
def pythonsum(n):
a = list(range(n))
b = list(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
size = int(sys.argv[1])
start = datetime.now()
c = pythonsum(size)
delta = datetime.now() - start
print("The last 2 elements of the sum", c[-2:])
print("PythonSum elapsed time in microseconds", delta.microseconds)
start = datetime.now()
c = numpysum(size)
delta = datetime.now() - start
print("The last 2 elements of the sum", c[-2:])
print("NumPySum elapsed time in microseconds", delta.microseconds)
|
执行结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | $ python3 vectorsum.py 100
The last 2 elements of the sum [950796, 980100]
PythonSum elapsed time in microseconds 167
The last 2 elements of the sum [950796 980100]
NumPySum elapsed time in microseconds 85
$ python3 vectorsum.py 1000
The last 2 elements of the sum [995007996, 998001000]
PythonSum elapsed time in microseconds 1456
The last 2 elements of the sum [995007996 998001000]
NumPySum elapsed time in microseconds 103
$ python3 vectorsum.py 10000
The last 2 elements of the sum [999500079996, 999800010000]
PythonSum elapsed time in microseconds 14696
The last 2 elements of the sum [999500079996 999800010000]
NumPySum elapsed time in microseconds 455
|
从结果看来,相对于python自带的列表,numpy在大数据量的情况下比较有性能优势。