《NumPy Beginner's Guide》笔记Chapter1
2016-03-08 23:23
309 查看
# -*- coding: utf-8 -*- #日期 = 23:33 """ 1. Imagine that we want to add two vectors called a and b The vector a holds the squares of integers 0 to n, for instance, if n is equal to 3, then a is equalto 0, 1, or 4. The vector b holds the cubes of integers 0 to n, so if n is equal to 3, then the vector b is equal to 0, 1, or 8. """ #plain Python 实现 def 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 c #numpy 实现 import numpy as np def numpysum(n): a = np.arange(n) ** 2 b = np.arange(n) ** 3 c = a + b return c import time tic = time.time() for i in range(10000): #循环10000次 c = pythonsum(1000) toc = time.time() print("The execute time of python is %f s" % (toc - tic)) tic = time.time() for i in range(10000): #循环10000次 c = numpysum(1000) toc = time.time() print("The execute time of numpy is %f s" % (toc - tic)) """ 输出: The execute time of python is 3.842000 s The execute time of numpy is 0.916000 s """
相关文章推荐
- 利用Python和OpenCV库将URL转换为OpenCV格式的方法
- Python NumPy库安装使用笔记
- Python中的Numpy入门教程
- NumPy基础(一)
- 安装numpy matplotlib
- python+numpy+scipy=matlab,抛弃matlab
- Python安装matplotlib所需要的软件以及问题的解决方法
- 例2.1.1的Numpy实现
- matplotlib初试——求两条直线相交的大概位置
- 绘制弹性模量的变化
- theano学习札记
- ParaView - Mixing NumPy and VTK APIs with high efficient using Python Programmable filter
- 开始正式写博客的第一天
- 新手学NumPy
- 创建视图和副本
- windows下安装Python以及numpy、scipy、scitik-learn模块(3.4版本)
- python几个工具包的安装
- openblas+lapack numpy scipy
- NumPy 模块 常用方法 学习
- python中import module问题