# >>> np.arange(3)
# array([0, 1, 2])
# >>> np.arange(3.0)
# array([ 0.,  1.,  2.])
# >>> np.arange(3,7)
# array([3, 4, 5, 6])
# >>> np.arange(3,7,2)
# array([3, 5])
# 尝试使用 range()创建整数列表(导致“TypeError: 'range' object does not support item assignment”)
#
# 有时你想要得到一个有序的整数列表,所以 range() 看上去是生成此列表的不错方式。然而,你需要记住 range() 返回的是 “range object”,而不是实际的 list 值。
#
# 该错误发生在如下代码中:
#
# spam = range(10)
# spam[4] = -1
# 也许这才是你想做:
#
# spam = list(range(10))
# spam[4] = -1
import datetime
import numpy as np
def numpysum(n):
    a = np.arange(n) ** 2
    b = np.arange(n) ** 3
    c = a + b
    return a, b, 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 a, b, c
def main():
    start = datetime.datetime.now()
    s1 = numpysum(10)
    print(s1)
    print((datetime.datetime.now() - start).microseconds)
    start = datetime.datetime.now()
    s2 = pythonsum(10)
    print(s2)
    print((datetime.datetime.now() - start).microseconds)
if __name__ == '__main__':
    main()
/home/livingbody/PycharmProjects/knn00/venv/bin/python /usr/local/pycharm-2019.1.1/helpers/pydev/pydevconsole.py --mode=client --port=45691
import sys; print('Python %s on %s' % (sys.version, sys.platform))
sys.path.extend(['/home/livingbody/PycharmProjects/knn00'])
Python 3.5.3 (default, Sep 27 2018, 17:25:39) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.6.1 -- An enhanced Interactive Python. Type '?' for help.
PyDev console: using IPython 7.6.1
Python 3.5.3 (default, Sep 27 2018, 17:25:39) 
[GCC 6.3.0 20170516] on linux
runfile('/home/livingbody/PycharmProjects/knn00/numpy/numpy00.py', wdir='/home/livingbody/PycharmProjects/knn00/numpy')
(array([ 0,  1,  4,  9, 16, 25, 36, 49, 64, 81]), array([  0,   1,   8,  27,  64, 125, 216, 343, 512, 729]), array([  0,   2,  12,  36,  80, 150, 252, 392, 576, 810]))
443
([0, 1, 4, 9, 16, 25, 36, 49, 64, 81], [0, 1, 8, 27, 64, 125, 216, 343, 512, 729], [0, 2, 12, 36, 80, 150, 252, 392, 576, 810])
21
看看443ms和21ms,明显比numpy有时间优势,这你怎么讲?