首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >可以进行一些python / numpy优化吗?

可以进行一些python / numpy优化吗?
EN

Stack Overflow用户
提问于 2013-11-19 02:26:58
回答 1查看 764关注 0票数 3

我正在分析一些带有嵌套循环的遗传算法代码,从我看到的情况来看,大部分时间都花在了我的两个函数上,这两个函数涉及切片和累加numpy数组。我尽了最大的努力来进一步优化它们,但我想看看其他人是否会有想法。

功能1:

第一个函数被调用2954684次,在函数内部花费的总时间为19秒

基本上,我们只是根据data1中包含的坐标在数据中包含的numpy数组中创建视图

代码语言:javascript
复制
def get_signal(data, options):
    #data[0] contains bed, data[1] contains position
    #forward = 0, reverse = 1
    start = data[1][0] - options.halfwinwidth
    end = data[1][0] + options.halfwinwidth
    if data[1][1] == 0:
        normals_forward = data[0]['normals_forward'][start:end]
        normals_reverse = data[0]['normals_reverse'][start:end]
    else:
        normals_forward = data[0]['normals_reverse'][end - 1:start - 1: -1]
        normals_reverse = data[0]['normals_forward'][end - 1:start - 1: -1]

    row = {'normals_forward': normals_forward,
           'normals_reverse': normals_reverse,
           }
    return row

功能2:

调用857次,在函数内部花费的总时间为13.674秒:

signal是长度相等的numpy数组列表,dtype是float,options只是随机选项

该函数的目标只是将每个numpy数组的列表相加为一个数组,计算正向数组和反向数组形成的两条曲线的交集并返回结果

代码语言:javascript
复制
def calculate_signal(signal, options):

    profile_normals_forward = np.zeros(options.halfwinwidth * 2, dtype='f')
    profile_normals_reverse = np.zeros(options.halfwinwidth * 2, dtype='f')

    #here i tried np.sum over axis = 0, its significantly slower than the for loop approach
    for b in signal:
        profile_normals_forward += b['normals_forward']
        profile_normals_reverse += b['normals_reverse']

    count = len(signal)

    if options.normalize == 1:
        #print "Normalizing to max counts"
        profile_normals_forward /= max(profile_normals_forward)
        profile_normals_reverse /= max(profile_normals_reverse)
      elif options.normalize == 2:
        #print "Normalizing to number of elements"
        profile_normals_forward /= count
        profile_normals_reverse /= count

    intersection_signal = np.fmin(profile_normals_forward, profile_normals_reverse)
    intersection = np.sum(intersection_signal)

    results = {"intersection": intersection,
               "profile_normals_forward": profile_normals_forward,
               "profile_normals_reverse": profile_normals_reverse,
               }
    return results

正如您所看到的,这两个非常简单,但在一个可以运行几个小时/几天(遗传算法优化)的脚本上占我执行时间的60%以上,所以即使是很小的改进也是受欢迎的:)

EN

回答 1

Stack Overflow用户

发布于 2013-12-17 07:47:03

为了提高第一个函数的速度,我要做的一件简单的事情就是使用不同的符号来访问列表索引,作为详细的here

例如:

代码语言:javascript
复制
foo = numpyArray[1][0] 
bar = numpyArray[1,0]

第二行的执行速度会快得多,因为您不必返回numpyArray[1]处的整个元素,然后再找到其中的第一个元素。试试看

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20055067

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档