【问题标题】:numpy array processing takes more time than list processing pythonnumpy 数组处理比列表处理 python 花费更多时间
【发布时间】:2018-08-18 02:45:41
【问题描述】:

我正在尝试比较 python 列表和 numpy 数组的处理时间。 我知道 numpy 数组比 python 列表快,但是当我实际检查时,我得到列表比 numpy 数组快。

这是我的代码:

import numpy
from datetime import datetime

def pythonsum(n):  
    '''
    This function  calculates the sum of two python list
    ''' 
    a = list(range(n))
    b = list(range(n))
    c = []
    total = 0

    for i in range(len(a)):       
        a[i] = i ** 2
        b[i] = i ** 3       
        c.append(a[i] + b[i])
    return c

def numpysum(n):
    '''
    This function  calculates the sum of two numpy array
    ''' 
    a  = numpy.arange(n) ** 2
    b = numpy.arange(n) ** 3

    c = a + b
    return c 

if __name__ == "__main__":
    n = int(input("enter the range"))

    start = datetime.now()
    result = pythonsum(n)
    delta = datetime.now()-start
    print("time required by pythonsum is",delta.microseconds)

    start = datetime.now()
    result1 = numpysum(n)
    delta = datetime.now()-start
    print("time required by numpysum is",delta.microseconds)

    delta = datetime.now()-start
    print("time required by numpysum is",delta.microseconds)

输出:

In [32]: run numpy_practice.py
enter the range7
time required by pythonsum is 0
time required by numpysum is 1001

【问题讨论】:

  • 如果您将范围扩大到... 10000000,会发生什么?
  • 如果你想为函数、脚本或其他东西计时,你想使用timeit。见this thread
  • 嗨大卫,请看输出:在 [55] 中:运行 numpy_practice.py 输入范围 10000000 pythonsum 所需的时间为 766959 numpysum 所需的时间为 123327 在 [56] 中:运行 numpy_practice.py 输入pythonsum所需的range5时间为0 numpysum所需的时间为0 在[57]中:运行numpy_practice.py输入range4000 pythonsum所需的时间为4812 numpysum所需的时间为0 在[58]中:运行numpy_practice.py输入range40时间pythonsum所需时间为0 numpysum所需时间为496
  • 你能检查一下上次运行,当我进入40范围时,pythonsum消耗的时间是0,而numpysum是496。它表明numpysum比pythonsum慢。无法理解背后的原因。
  • 我会指出,在某些情况下列表更快,通常是如果您的数据是非数字的(例如stackoverflow.com/questions/49112552/…

标签: python numpy


【解决方案1】:

将样板下面的代码更改为此并改用timeit 模块。此代码允许我多次执行函数并返回平均值和标准偏差执行时间。

if __name__ == "__main__":
    import timeit
    n = int(input("enter the range"))
    setup = """\
import numpy as np
from __main__ import numpysum,pythonsum
n = {iterations}

    """.format(iterations=n)

    npex = numpy.array(timeit.repeat('numpysum(n)',setup=setup,repeat=100,number=1000))
    pyex = numpy.array(timeit.repeat('pythonsum(n)',setup=setup,repeat=100,number=1000))
    print("Numpy Execution Time, mean={m},std={sd}".format(m=npex.mean(),sd=npex.std(0)))
    print("Python Execution Time, mean={m},std={sd}".format(m=pyex.mean(), sd=pyex.std(0)))

通过使用 n = 100 进行测试,很明显 numpy 的速度要快得多

enter the range100
Numpy Execution Time, mean=0.00482892623162404,std=0.0005752600215192671
Python Execution Time, mean=0.1037349765653833,std=0.004933817536363718

【讨论】:

  • 谢谢 DJK,请您澄清一下,n = {iterations} 是什么意思。
  • @Dipti C,当然。 timeit 接受一个参数设置,所以如果我们需要代码来设置定时代码,我们将它写在一个字符串中并将它传递给它。 .format 允许我们在存在 {} 的字符串中插入值,为了清楚起见,我只是命名了变量迭代,n 在此代码中的作用与在您的代码中的作用相同。
  • @DipitiC ,您会考虑将其作为公认的解决方案吗?
猜你喜欢
  • 2013-11-09
  • 1970-01-01
  • 2018-08-30
  • 2016-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-19
  • 2017-01-24
相关资源
最近更新 更多