我的数组操作比我的列表操作慢。

huangapple go评论57阅读模式
英文:

My Array manipulations are slower than my list manipulations

问题

我想绘制一些随机漫步图我听说 numpy 数组比列表快得多但是我的数组操作比我的列表操作要花两到三十倍的时间我做错了什么

from random import random
import numpy as np
import matplotlib.pyplot as plt
import time

t0 = time.time()

rw1 = list()
rw1.append(-1 if random() < 0.5 else 1)
for i in range(1, 10000):
    movement = -1 if random() < 0.5 else 1
    rw1.append(rw1[i-1] + movement)

t1 = time.time()
rw2 = [0]
rw2_array = np.array(rw2, dtype=int)

for i in range(1, 10000):
    movement = -1 if random() < 0.5 else 1
    rw2_array = np.append(rw2_array, rw2_array[i-1] + movement)

t2 = time.time()

rw3 = np.zeros(10000)

for i in range(1, 10000):
    movement = -1 if random() < 0.5 else 1
    rw3[i] = rw3[i-1] + movement

t3 = time.time()
print("time:", t1-t0, t2-t1, t3-t2)
英文:

I want to plot some random walks. I have heard that numpy arrays are alot faster than lists. But my array manipulations take twice to thirty times as long as my list manipulations. What am i doing wrong?

from random import random
import numpy as np
import matplotlib.pyplot as plt
import time

t0 = time.time()

rw1 = list()
rw1.append(-1 if random() &lt; 0.5 else 1)
for i in range(1, 10000):
    movement = -1 if random() &lt; 0.5 else 1
    rw1.append(rw1[i-1] + movement)

t1 = time.time()
rw2 = [0]
rw2_array = np.array(rw2, dtype=int)

for i in range(1, 10000):
    movement = -1 if random() &lt; 0.5 else 1
    rw2_array = np.append(rw2_array, rw2_array[i-1] + movement)

t2 = time.time()

rw3 = np.zeros(10000)

for i in range(1, 10000):
    movement = -1 if random() &lt; 0.5 else 1
    rw3[i] = rw3[i-1] + movement

t3 = time.time()
print(&quot;time: &quot;, t1-t0, t2-t1, t3-t2)

答案1

得分: 1

你不应该在numpy数组上进行迭代。而是应该构建一个移动数组,然后计算随机漫步。我们可以通过在大小为10000的数组中随机选择+1或-1来构建移动数组。要获得随机漫步,可以使用累积和方法。

import numpy as np

movements = np.random.choice([-1, 1], size=10000)
rw = movements.cumsum()

这些操作在C层进行,比在本机Python中进行的操作要快得多。通过在numpy数组上进行迭代,你将整数从C中取出,并为每个数字创建一个Python对象。由于创建每个对象时的开销,这个过程较慢。

通过使用numpy中的函数和方法,你可以传递调用到C层,在那里执行所有操作。

英文:

You should not be iterating over numpy arrays. Instead build the array of movements and then calculate the random walk. We can build the movements by randomly selecting +1 or -1 for an array of size 10000. To get the random walk, you can use the cumulative sum method.

import numpy as np

movements = np.random.choice([-1, 1], size=10000)
rw = movements.cumsum()

The operations take place at the C layer and are significantly faster than operations in native Python. By iterating over the numpy array, you are taking the integer out of C and creating a Python object for each number. This process is slow because of the overhead in creating each object.

By using the functions and methods in numpy, you are passing calls to the C layer to do all of the operations there.

huangapple
  • 本文由 发表于 2023年6月5日 20:50:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76406587.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定