gpt4 book ai didi

python - 随机游走代码 python

转载 作者:行者123 更新时间:2023-11-28 17:27:48 24 4
gpt4 key购买 nike

你多冷加速下面的代码能做到100个人以上?

""" Random Walker """
import numpy as np
import scipy as sp
import random as rd
import time

def procedure():
time.sleep(2.5)
t0C = time.clock()
t0 = time.time()

"""Definitions"""

def ifilterfalse(predicate, iterable):
# ifilterfalse(lambda x: x%2, range(10)) --> 0 2 4 6 8
if predicate is None:
predicate = bool
for x in iterable:
if not predicate(x):
yield x

def unique_everseen(iterable, key=None):
"List unique elements, preserving order. Remember all elements ever seen."
# unique_everseen('AAAABBBCCDAABBB') --> A B C D
# unique_everseen('ABBCcAD', str.lower) --> A B C D
seen = set()
seen_add = seen.add
if key is None:
for element in ifilterfalse(seen.__contains__, iterable):
seen_add(element)
yield element
else:
for element in iterable:
k = key(element)
if k not in seen:
seen_add(k)
yield element

"""Creating the Random Walk"""

n=int(input('Number of Individuals at Table: '))
iters=10000
final=np.zeros(n)
total=0
for j in xrange(iters):
d=np.array([0])
i=0
while i<1:
new=d[len(d)-1]+rd.choice([-1,1])
if new<0:
new+=n
elif new>=n:
new-=n
d=np.append(d,new)
dshort=list(unique_everseen(d))
if len(dshort)>=n:
i=1
last=dshort[len(dshort)-1]
length=len(d)
final[last]+=1
total+=length

final=np.round(final/iters,4)
total=round(total/iters,3)

"""Writing To A File"""

print (40 * '-')
print (" ")
print (" Percentages: ")
print (" ")
print (" S#:"+" S#:".join(map(str,range(n))))
print (" "+"% ".join(map(str,final))+"%")
print (" ")
print (" Average Number of Passes of Plate: {}".format(total))
print (" ")
print (40 * '-')

# measure process time
print time.clock() - t0C, "seconds process time"

# measure wall time
print time.time() - t0, "seconds wall time"


if __name__ == "__main__":
procedure()

现在对于 10 个人的情况,时间是:

5.877529 秒处理时间

12.9134569168 秒墙时间

问题是当个人数量(100、1000)增加时代码太慢,有什么建议吗?

最佳答案

问题是 unique_everseen在连续的执行中做几乎相同的工作,消耗了太多时间。这是一个删除了 unique_everseen 的简化版本功能和d列出并使用 seen直接在主循环和last中设置var 保留最后一项:

""" Random Walker """
import random as rd
import time

def procedure():
n = int(input('Number of Individuals at Table: '))

t0C = time.clock()
t0 = time.time()

iters = 10000
final = [0] * n
total = 0
for j in xrange(iters):
last = 0
count = 1
seen = set([0])
while len(seen) < n:
count += 1;
new = last + rd.choice([-1, 1])
if new < 0:
new += n
elif new >= n:
new -= n
seen.add(new)
last = new
final[last] += 1
total += count

final = [round(float(f) / iters, 4) for f in final]
total = round(float(total) / iters, 3)

"""Writing To A File"""

print(40 * '-')
print(" ")
print(" Percentages: ")
print(" ")
print(" S#:" + " S#:".join(map(str, range(n))))
print(" " + "% ".join(map(str, final)) + "%")
print(" ")
print(" Average Number of Passes of Plate: {}".format(total))
print(" ")
print(40 * '-')

# measure process time
print time.clock() - t0C, "seconds process time"

# measure wall time
print time.time() - t0, "seconds wall time"


if __name__ == "__main__":
procedure()

请注意删除 numpy依赖项允许使用 pypy 运行脚本

一些结果(以秒为单位)

  • 10 个人
    • python :0.472
    • pypy:0.084
  • 100 人
    • python :49.352
    • pypy:3.256
  • 500 人
    • pypy:80.460
  • 1000 人
    • pypy:318.392

关于python - 随机游走代码 python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37263565/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com