小编Nis*_*ssl的帖子

Numpy/Python与Matlab相比表现非常糟糕

新手程序员在这里.我正在编写一个程序来分析点(单元格)的相对空间位置.程序从数组中获取边界和单元格类型,第1列中的x坐标,第2列中的y坐标和第3列中的单元格类型.然后,它会检查每个单元格的单元格类型以及与边界的适当距离.如果它通过,则计算它与阵列中每个其他单元格的距离,如果距离在指定的分析范围内,则将其添加到该距离的输出数组.

我的单元格标记程序是在wxpython中,所以我希望在python中开发这个程序并最终将它粘贴到GUI中.不幸的是,现在python在我的机器上运行核心循环需要大约20秒,而MATLAB可以执行~15循环/秒.由于我计划在大约30个案例中对几个探索性分析类型进行1000次循环(具有随机比较条件),这不是一个微不足道的差异.

我尝试运行一个分析器,数组调用是1/4的时间,几乎所有其余的都是未指定的循环时间.

这是主循环的python代码:

for basecell in range (0, cellnumber-1):
    if firstcelltype == np.array((cellrecord[basecell,2])):
        xloc=np.array((cellrecord[basecell,0]))
        yloc=np.array((cellrecord[basecell,1]))
        xedgedist=(xbound-xloc)
        yedgedist=(ybound-yloc)
        if xloc>excludedist and xedgedist>excludedist and yloc>excludedist and    yedgedist>excludedist:
            for comparecell in range (0, cellnumber-1):
                if secondcelltype==np.array((cellrecord[comparecell,2])):
                    xcomploc=np.array((cellrecord[comparecell,0]))
                    ycomploc=np.array((cellrecord[comparecell,1]))
                    dist=math.sqrt((xcomploc-xloc)**2+(ycomploc-yloc)**2)
                    dist=round(dist)
                    if dist>=1 and dist<=analysisdist:
                         arraytarget=round(dist*analysisdist/intervalnumber)
                         addone=np.array((spatialraw[arraytarget-1]))
                         addone=addone+1
                         targetcell=arraytarget-1
                         np.put(spatialraw,[targetcell,targetcell],addone)
Run Code Online (Sandbox Code Playgroud)

这是主循环的matlab代码:

for basecell = 1:cellnumber;
    if firstcelltype==cellrecord(basecell,3);
         xloc=cellrecord(basecell,1);
         yloc=cellrecord(basecell,2);
         xedgedist=(xbound-xloc);
         yedgedist=(ybound-yloc);
         if (xloc>excludedist) && (yloc>excludedist) && (xedgedist>excludedist) && (yedgedist>excludedist);
             for comparecell = 1:cellnumber;
                 if secondcelltype==cellrecord(comparecell,3);
                     xcomploc=cellrecord(comparecell,1);
                     ycomploc=cellrecord(comparecell,2);
                     dist=sqrt((xcomploc-xloc)^2+(ycomploc-yloc)^2);
                     if (dist>=1) && (dist<=100.4999);
                         arraytarget=round(dist*analysisdist/intervalnumber); …
Run Code Online (Sandbox Code Playgroud)

python matlab numpy

8
推荐指数
1
解决办法
4371
查看次数

标签 统计

matlab ×1

numpy ×1

python ×1