Performance Python—为什么这段代码需要这么长时间?
我关心的是两个散列行之间的部分。以下代码运行时间太长,我无法等待其输出。当我用另一段代码替换有问题的部分时,程序只需几秒钟就可以运行(见本文末尾)。我的目标是生成90个均匀分布在单位正方形中的数据点(Performance Python—为什么这段代码需要这么长时间?,performance,python-2.7,parallel-processing,random-sample,embarrassingly-parallel,Performance,Python 2.7,Parallel Processing,Random Sample,Embarrassingly Parallel,我关心的是两个散列行之间的部分。以下代码运行时间太长,我无法等待其输出。当我用另一段代码替换有问题的部分时,程序只需几秒钟就可以运行(见本文末尾)。我的目标是生成90个均匀分布在单位正方形中的数据点(var1,var2),然后生成12个随机放置在半径为1/8的圆中的点,该圆完全位于单位正方形内(cir1,cir2),最后将这两个点集连接起来(sph1,sph2) 从昨天的广告开始,我就一直在挖掘这段代码,我确信它是正确的(显然不是)。我可能遗漏了一些非常明显的东西 #!/usr/bin/env
var1
,var2
),然后生成12个随机放置在半径为1/8的圆中的点,该圆完全位于单位正方形内(cir1
,cir2
),最后将这两个点集连接起来(sph1
,sph2
)
从昨天的广告开始,我就一直在挖掘这段代码,我确信它是正确的(显然不是)。我可能遗漏了一些非常明显的东西
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import random
import math
import numpy as np
import sys
from heapq import merge
import multiprocessing as mp
from multiprocessing import Pool
boot = 2
RRpoints = 90
rrpoints = 12
step = np.arange(-8.0 , 0.5 , 0.5)
def distance_between_points(ite, jte):
xi, yi = ite
xj, yj = jte
x2 = (xi - xj) ** 2
y2 = (yi - yj) ** 2
d = math.sqrt(x2 + y2)
return d
def heaviside_step(n):
return int(n >= 0)
def myscript(iteration_number):
RRfile_name = "MC_out_cluster_1/output%d.txt" % iteration_number
with open(RRfile_name, "w") as RRf:
#############################################################################
np.random.seed()
var1 = np.random.uniform(0, 1 , RRpoints)
var2 = np.random.uniform(0, 1 , RRpoints)
cir1 = []
cir2 = []
x0 = np.random.uniform(0.125 , 0.875)
y0 = np.random.uniform(0.125 , 0.875)
while ( len(cir1) < rrpoints and len(cir2) < rrpoints ):
np.random.seed()
col1 = np.random.uniform(x0 - 0.125 , x0 + 0.125)
col2 = np.random.uniform(y0 - 0.125 , y0 + 0.125)
if (x0 - col1) ** 2 + (y0 - col2) ** 2 <= 1/64:
cir1.append(col1)
cir2.append(col2)
sph1 = list(merge(var1 , cir1))
sph2 = list(merge(var2 , cir2))
############################################################################
corr = []
for k in xrange(0, len(step)):
h = 0
for i in xrange(0, RRpoints):
for j in xrange(1 + i, RRpoints):
ite = sph1[i] , sph2[i]
jte = sph1[j] , sph2[j]
dbp = distance_between_points(ite, jte)
h += heaviside_step(math.exp( step[k] ) - dbp)
corr.append([math.exp(step[k]) , h])
for item in corr:
RRf.write("{0}\t{1}\n".format(item[0], item[1]))
x = xrange(boot)
p = mp.Pool()
y = p.imap(myscript, x)
list(y)
!/usr/bin/env python
#-*-编码:utf-8-*-
随机输入
输入数学
将numpy作为np导入
导入系统
从heapq导入合并
将多处理作为mp导入
来自多处理导入池
启动=2
rR点=90
rr分=12分
阶跃=np.arange(-8.0,0.5,0.5)
def点之间的距离(ite,jte):
席,伊利石
xj,yj=jte
x2=(xi-xj)**2
y2=(yi-yj)**2
d=数学sqrt(x2+y2)
返回d
def heaviside_步骤(n):
返回整数(n>=0)
def myscript(迭代编号):
RRfile\u name=“MC\u out\u cluster\u 1/输出%d.txt”%迭代编号
打开(RRU文件名,“w”)作为RRf:
#############################################################################
np.random.seed()
var1=np.随机.均匀(0,1,rr点)
var2=np.随机.均匀(0,1,rr点)
cir1=[]
cir2=[]
x0=np.随机均匀(0.125,0.875)
y0=np.随机均匀(0.125,0.875)
而(len(cir1)1/16):
var1.append(col1)
var2.append(col2)
cir1=[]
cir2=[]
而(len(cir1)=0.140625和(new1-1)**2+(new2-0.5)**2>=0.140625):
cir1.追加(新1)
cir2.追加(新2)
sph1=列表(合并(var1,cir1))
sph2=列表(合并(var2,cir2))
#############################################################################
您使用的是Python2.x,因此1/64==0
(在Python2中,两个整数的除法得到一个整数结果)。如果可以,请升级到Python3.x,否则将测试更改为使用1./64
您使用的是Python2.x,因此1/64==0
(在Python2中,两个整数的除法给出一个整数结果)。如果可以,升级到Python3.x,否则将测试更改为使用1./64
您使用的是哪个版本的Python?您还可以使用x=sqrt(r)*cos(θ)更有效地在圆中生成随机点;y=sqrt(r)*sin(θ)
。请参见,和,.您使用的是哪种版本的python?您还可以使用x=sqrt(r)*cos(θ)更有效地生成圆中的随机点;y=sqrt(r)*sin(θ)
。请参阅,以及。
#############################################################################
var1 = []
var2 = []
while (len(var1) < RRpoints):
np.random.seed()
col1 = np.random.uniform(0 , 1)
col2 = np.random.uniform(0 , 1)
if ( col1 ** 2 + (col2 - 0.5) ** 2 > 1/16 and (col1 - 1) ** 2 + (col2 - 0.5) ** 2 > 1/16 ):
var1.append(col1)
var2.append(col2)
cir1 = []
cir2 = []
while (len(cir1) < rrpoints and len(cir2) < rrpoints):
np.random.seed()
new1 = np.random.uniform(0.125 , 0.875)
new2 = np.random.uniform(0.125 , 0.875)
if ( new1 ** 2 + (new2 - 0.5) ** 2 >= 0.140625 and (new1 - 1) ** 2 + (new2 - 0.5) ** 2 >= 0.140625 ):
cir1.append(new1)
cir2.append(new2)
sph1 = list(merge(var1 , cir1))
sph2 = list(merge(var2 , cir2))
#############################################################################