简体   繁体   English

为什么 numpy 数组比列表花费更多时间?

[英]Why numpy array takes more time than list?

Why is numpy slower in this code?为什么 numpy 在此代码中较慢?

for i in range(10000):
    array = [[0.0,] * 1024 for x in range(1024)]

0,021539204 seconds time elapsed (39.616.810 instructions)经过 0,021539204 秒的时间(39.616.810 指令)

import numpy as np
for i in range(10000):
    array = np.zeros((1024,1024))

0.209111860 seconds time elapsed (1.067.923.180 instructions)经过 0.209111860 秒的时间(1.067.923.180 指令)

Are you running in the exact same machine?您是否在完全相同的机器上运行? I'm getting faster result in numpy .我在numpy的结果越来越快。

In [7]: %%time
   ...: import numpy as np
   ...: for i in range(10000):
   ...:     array = np.zeros((1024,1024))
   ...:
CPU times: user 3.33 s, sys: 0 ns, total: 3.33 s
Wall time: 3.32 s

In [8]: %%time
   ...: for i in range(10000):
   ...:     array = [[0.0,] * 1024 for x in range(1024)]
   ...:
CPU times: user 1min 14s, sys: 0 ns, total: 1min 14s
Wall time: 1min 14s

This answer in the numpy vs list thread also agrees. numpy vs list线程中的这个答案也同意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 numpy数组处理比列表处理python需要更多时间 - numpy array processing takes more time than list processing python 为什么 numpy 数组比 python 中的列表快? - Why numpy array is faster than list in python? 为什么 Pandas 中的 groupby().agg(list_funcs) 函数比单独使用函数列表花费更多的时间? - Why does groupby().agg(list_funcs) function in Pandas takes significantly more time with a list of functions, than using them individually? 映射到 numpy 数组,为什么要花更多时间在一起? - maps to numpy array, why is taking more time all together? 为什么原生python列表上的for循环比numpy数组上的for循环快 - Why is for loop on native python list faster than for loop on numpy array 为什么 Python 列表上的 `for` 比 Numpy 数组更快? - Why is a `for` over a Python list faster than over a Numpy array? 为什么列表操作比numpy数组操作慢 - Why operation on list is slower than operation on numpy array 蛮力比二分查找需要更多时间来查找排序列表的第一个元素 - Bruteforce takes more time than binary search to find the first element of a sorted list 为什么与 DASK Delayed 合并比与 DASK 内置命令合并需要更多时间? - Why merging with DASK Delayed takes extremely more time than merging with DASK built-in command? 为什么 Numpy 阵列重塑需要两个括号? - Why Numpy array reshape takes two brackets?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM