如何加快此Python代码的速度? [英] How to speed up this Python code?

查看:130
本文介绍了如何加快此Python代码的速度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下微型Python方法,它是 到目前为止 的性能热点(根据我的分析器,在此花费了95%以上的执行时间)一个更大的程序:

I've got the following tiny Python method that is by far the performance hotspot (according to my profiler, >95% of execution time is spent here) in a much larger program:

def topScore(self, seq):
    ret = -1e9999
    logProbs = self.logProbs  # save indirection
    l = len(logProbs)
    for i in xrange(len(seq) - l + 1):
        score = 0.0
        for j in xrange(l):
            score += logProbs[j][seq[j + i]]
        ret = max(ret, score)

    return ret

如果重要的话,代码将在Python的Jython实现中运行,而不是CPython. seq是一个DNA序列字符串,数量为1,000个元素. logProbs是词典的列表,每个位置一个.目的是找到seq的任何长度l(约10-20个元素)子序列的最大分数.

The code is being run in the Jython implementation of Python, not CPython, if that matters. seq is a DNA sequence string, on the order of 1,000 elements. logProbs is a list of dictionaries, one for each position. The goal is to find the maximum score of any length l (on the order of 10-20 elements) subsequence of seq.

我意识到所有这些循环由于解释开销而效率低下,并且在静态编译/JIT语言中会快很多.但是,我不愿意切换语言.首先,对于正在使用的库,我需要一种JVM语言,而这种情况限制了我的选择.其次,我不想将此代码批量翻译为较低级别的JVM语言.但是,尽管我不知道如何进行接口连接或会产生多少开销,我还是愿意在必要时用其他方式重写此热点.

I realize all this looping is inefficient due to interpretation overhead and would be a heck of a lot faster in a statically compiled/JIT'd language. However, I'm not willing to switch languages. First, I need a JVM language for the libraries I'm using, and this kind of constrains my choices. Secondly, I don't want to translate this code wholesale into a lower-level JVM language. However, I'm willing to rewrite this hotspot in something else if necessary, though I have no clue how to interface it or what the overhead would be.

除了这种方法的单线程速度慢之外,就并行化而言,我也无法使程序扩展到超过4个CPU.鉴于它几乎将所有时间都花在我发布的10行热点上,因此我无法弄清楚这里可能存在的瓶颈.

In addition to the single-threaded slowness of this method, I also can't get the program to scale much past 4 CPUs in terms of parallelization. Given that it spends almost all its time in the 10-line hotspot I've posted, I can't figure out what the bottleneck could be here.

推荐答案

如果对同一seq重复调用topScore,则可以memoize其值.

if topScore is called repeatedly for same seq you could memoize its value.

例如 http://code.activestate.com/recipes/52201/

这篇关于如何加快此Python代码的速度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆