zoukankan      html  css  js  c++  java
  • Python数据分析学习(一):Numpy与纯Python计算向量加法速度比较

    import sys
    from datetime import datetime
    import numpy as np
    
    
    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)

    运行结果: 

    python vectorsum.py 100000
    The last 2 elements of the sum [999950000799996, 999980000100000]
    PythonSum elapsed time in microseconds  91446
    The last 2 elements of the sum [999950000799996 999980000100000]
    NumPySum elapsed time in microseconds  2824
    
    
    python vectorsum.py 200000
    The last 2 elements of the sum [7999800001599996, 7999920000200000]
    PythonSum elapsed time in microseconds  178237
    The last 2 elements of the sum [7999800001599996 7999920000200000]
    NumPySum elapsed time in microseconds  6453
    
    
    python vectorsum.py 300000
    The last 2 elements of the sum [26999550002399996, 26999820000300000]
    PythonSum elapsed time in microseconds 264677
    The last 2 elements of the sum [26999550002399996 26999820000300000]
    NumPySum elapsed time in microseconds 9951

  • 相关阅读:
    yii2增删改查及AR的理解
    yii2中关联查询
    yii2常用的migrate命令
    有线电视网
    选课
    没有上司的舞会
    [ZJOI2008]骑士
    【模板】树链剖分
    [ZJOI2008]树的统计
    [NOI2015]软件包管理器
  • 原文地址:https://www.cnblogs.com/prince5460/p/8483474.html
Copyright © 2011-2022 走看看