Python - 为什么这段代码需要这么长时间?

时间:2016-03-30 12:24:41

标签: performance python-2.7 parallel-processing random-sample embarrassingly-parallel

我担心的是两个哈希行之间的部分。以下代码运行时间太长,我无法等待其输出。当我用另一块代码替换有问题的部分时,程序会在几秒钟内运行(参见本文末尾)。我的目标是生成90个数据点,这些数据点均匀分布在单位正方形(var1var2)中,然后生成12个点,这些点随机放置在半径为1/8的圆中,完全位于在单位正方形(cir1cir2)内,最后加入这两组(sph1sph2)。

我从昨天开始就一直在挖掘这段代码,我确信它是正确的(显然它不是)。我可能错过了一些非常明显的东西......

#!/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)

这是我修改为创建上述代码的另一个(工作)块:

 #############################################################################
        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))
 #############################################################################

1 个答案:

答案 0 :(得分:2)

你正在使用Python 2.x所以1/64==0(在Python 2中,两个整数的除法给出了整数结果)。如果可以,请升级到Python 3.x,否则会更改测试以使用1./64