简体   繁体   English

为什么 numpy 数组比 python 中的列表快?

[英]Why numpy array is faster than list in python?

I have recently discovered that numpy array is faster than normal list in python.I have run the code below我最近发现 numpy 数组比 python 中的普通列表快。我运行了下面的代码

import numpy as np

import matplotlib.pyplot as plt

import pandas as pd

my_listy = list(range(1000000))

my_array = np.array(range(1000000))

%time for i in range(10): my_list2 = my_listy * 2

%time for i in range(10): my_array2 = my_array * 2

I found that execution time for the list was approx 425 ms whereas the execution time for the numpy array was approx 371 ms.我发现列表的执行时间约为 425 毫秒,而 numpy 数组的执行时间约为 371 毫秒。 Whats the specific reason for this??这具体是什么原因??

You are performing two completely different operations here so you cannot directly compare: multiplying a list by 2 will create a new list where the list is concatenated to itself (so the length of the output list is twice that of the input list), whereas multiplying a numpy array by 2 will create a new array of the same length as the original array, but in which each element has been multiplied by 2.您在这里执行两个完全不同的操作,因此您无法直接比较:将列表乘以 2 将创建一个新列表,其中列表连接到自身(因此 output 列表的长度是输入列表的两倍),而乘以numpy 数组乘以 2 将创建一个与原始数组长度相同的新数组,但其中每个元素都乘以 2。

Nonetheless, if you had attempted to perform a list operation which actually corresponds to the numpy case (element-by-element multiplication), for example:尽管如此,如果您尝试执行实际上对应于 numpy 案例(逐个元素乘法)的列表操作,例如:

my_list2 = [n * 2 for n in my_listy]

you would also have found that the numpy example was quicker.您还会发现 numpy 示例更快。 This is because the required looping in numpy is performed in a shared library consisting of compiled C code, rather than using an explicit loop in Python ( for loop or list comprehension).这是因为 numpy 中所需的循环是在由已编译的 C 代码组成的共享库中执行的,而不是在 Python 中使用显式循环( for loop or list comprehension)

$ python -mtimeit -s 'import numpy as np; my_array = np.array(range(1000000))' 'my_array2 = my_array * 2'
1000 loops, best of 3: 1.45 msec per loop

$ python -mtimeit -s 'my_listy = list(range(1000000))' 'my_list2 = [n*2 for n in my_listy]'
10 loops, best of 3: 50.8 msec per loop

The following are the main reasons behind the fast speed of Numpy.以下是Numpy速度快的主要原因。
-Numpy array is a collection of similar data-types that are densely packed in memory. -Numpy 数组是密集封装在 memory 中的类似数据类型的集合。 A Python list can have different data-types, which puts lots of extra constraints while doing computation on it. Python 列表可以具有不同的数据类型,这在对其进行计算时会产生许多额外的约束。
-Numpy is able to divide a task into multiple subtasks and process them parallelly. -Numpy 能够将一个任务分成多个子任务并并行处理。
-Numpy functions are implemented in C. -Numpy 函数在 C 中实现。 Which again makes it faster compared to Python Lists.与 Python 列表相比,这再次使其更快。

Python at first was not made for numeric operations but with time numpy was created for this scope(making python better at numeric operations). Python 最初不是为数字运算而设计的,但随着时间的推移,为这个范围创建了 numpy(使 python 在数字运算方面更好)。 Source: towardsdatascience.com来源: 朝向数据科学。com

In Numpy You have implementation in C++, that this is faster than pure python.在 Numpy 您在 C++ 中有实现,这比纯 python 快。

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

相关问题 为什么原生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数组看起来不比标准的python列表快得多? - Why does a numpy array not appear to be much faster than a standard python list? 为什么元组比 Python 中的列表快? - Why is tuple faster than list in Python? 为什么 python 列表中的索引比 pop 快? - Why is index faster than pop in python list? 为什么“in”生成器比python中的“in”列表快得多 - why "in" generator is much faster than "in" list in python 为什么Python中的“执行”操作比迭代列表要快? - Why is “in” operation in Python faster than iterating a list? 为什么列表理解比乘法数组的numpy要快得多? - Why list comprehension is much faster than numpy for multiplying arrays? 为什么遍历 Numpy 数组比直接操作更快 - why is iterating over a Numpy array faster than direct operations 为什么 numpy 的矩阵乘法比 Python 中的 ctypes 更快? - Why is matrix multiplication faster with numpy than with ctypes in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM