python dict计时之谜

2024-06-08 00:18:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在做序列比对,遇到了一个与dict数据结构的起源有关的相当神秘的时间问题。 基本上,我有函数alignment(s1, s2, scores) 它包含两个字符串s1和s2,以及一个评分矩阵(如python dict)来表示每对可能的20个氨基酸和一个gap'-'。所以scores有440个键(char1,char2),带有整数值。你知道吗

神秘之处在于:如果我从一个文本文件中读取scores(称之为scores1)并运行 alignment(s1, s2, scores1) 对于约1000个长度为s1、s2的氨基酸字符串,我得到以下计时(使用cProfile,不显示函数输出):

2537776 function calls in 11.796 seconds

现在,如果我在文件中创建完全相同的dict(称为scores2)并运行 alignment(s1, s2, scores2) 我得到了相同的输出结果,但时间减少了3倍:

2537776 function calls in 4.263 seconds

两种情况下的输出是相同的,只是时间不同。 运行print scores1 == scores2会产生True,因此它们包含相同的信息。 我验证了使用访问dict的任意函数(而不是对齐) 在这两种情况下,多次产生相同的3个时间差因子。你知道吗

一定有一些元数据与dict的起源有关,这会减慢我的功能(当从文件中读取时),即使在这两种情况下我实际上都在文件中读取。 我尝试通过scores1 = dict(scores1)等为每个dict对象创建一个新的dict对象,但是相同的时间差异仍然存在。很困惑,但我很肯定如果我能弄清楚的话,这会是一个很好的教训。你知道吗

scores1 = create_score_dict_from_file('lcs_scores.txt')
scores2 = create_score_dict(find_alp(s1, s2), match=1, mismatch=0, indel=0)
print scores1 == scores2   # True
alignment(s1, s2, scores1) # gives right answer in about 12s
alignment(s1, s2, scores2) # gives right answer in about 4s

编辑:添加以下代码和结果:

以下是代码的简化版本:

import numpy as np
from time import time

def create_scores_from_file(score_file, sigma=0):
    """
    Creates a dict of the scores for each pair in an alphabet,
    as well as each indel (an amino acid, paired with '-'), which is scored -sigma.

    """
    f = open(score_file, 'r')
    alp = f.readline().strip().split()
    scores = []
    for line in f:
        scores.append(map(int, line.strip().split()[1:]))
    f.close()
    scores = np.array(scores)
    score_dict = {}
    for c1 in range(len(alp)):
        score_dict[(alp[c1], '-')] = -sigma
        score_dict[('-', alp[c1])] = -sigma
        for c2 in range(len(alp)):
            score_dict[(alp[c1], alp[c2])] = scores[c1, c2]
    return score_dict

def score_matrix(alp=('A', 'C', 'G', 'T'), match=1, mismatch=0, indel=0):
    score_dict = {}
    for c1 in range(len(alp)):
        score_dict[(alp[c1], '-')] = indel
        score_dict[('-', alp[c1])] = indel
        for c2 in range(len(alp)):
            score_dict[(alp[c1], alp[c2])] = match if c1 == c2 else mismatch
    return score_dict

def use_dict_in_function(n, d):
    start = time()
    count = 0
    for i in xrange(n):
        for k in d.keys():
            count += d[k]
    print "Time: ", time() - start
    return count

def timing_test():
    alp = tuple('A  C  D  E  F  G  H  I  K  L  M  N  P  Q  R  S  T  V  W  Y'.split())
    scores1 = create_scores_from_file('lcs_scores.txt')
    scores2 = score_matrix(alp, match=1, mismatch=0, indel=0)
    print type(scores1), id(scores1)
    print type(scores2), id(scores2)
    print repr(scores1)
    print repr(scores2)
    print type(list(scores1)[0][0])
    print type(list(scores2)[0][0])
    print scores1 == scores2
    print repr(scores1) == repr(scores2)
    n = 10000
    use_dict_in_function(n, scores1)
    use_dict_in_function(n, scores2)

if __name__ == "__main__":
    timing_test()

结果是:

<type 'dict'> 140309927965024
<type 'dict'> 140309928036128
{('S', 'W'): 0, ('G', 'G'): 1, ('E', 'M'): 0, ('P', '-'): 0,... (440 key: values)
{('S', 'W'): 0, ('G', 'G'): 1, ('E', 'M'): 0, ('P', '-'): 0,... (440 key: values)
<type 'str'>
<type 'str'>
True
True
Time:  1.51075315475
Time:  0.352770090103

以下是lcs文件的内容_分数.txt地址:

   A  C  D  E  F  G  H  I  K  L  M  N  P  Q  R  S  T  V  W  Y
A  1  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  
C  0  1  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  
D  0  0  1  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  
E  0  0  0  1  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  
F  0  0  0  0  1  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  
G  0  0  0  0  0  1  0  0  0  0  0  0  0  0  0  0  0  0  0  0  
H  0  0  0  0  0  0  1  0  0  0  0  0  0  0  0  0  0  0  0  0  
I  0  0  0  0  0  0  0  1  0  0  0  0  0  0  0  0  0  0  0  0  
K  0  0  0  0  0  0  0  0  1  0  0  0  0  0  0  0  0  0  0  0  
L  0  0  0  0  0  0  0  0  0  1  0  0  0  0  0  0  0  0  0  0  
M  0  0  0  0  0  0  0  0  0  0  1  0  0  0  0  0  0  0  0  0  
N  0  0  0  0  0  0  0  0  0  0  0  1  0  0  0  0  0  0  0  0  
P  0  0  0  0  0  0  0  0  0  0  0  0  1  0  0  0  0  0  0  0  
Q  0  0  0  0  0  0  0  0  0  0  0  0  0  1  0  0  0  0  0  0  
R  0  0  0  0  0  0  0  0  0  0  0  0  0  0  1  0  0  0  0  0  
S  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  1  0  0  0  0  
T  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  1  0  0  0  
V  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  1  0  0  
W  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  1  0  
Y  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  1  

Tags: infortypedictscoreprintc2s2
1条回答
网友
1楼 · 发布于 2024-06-08 00:18:34

哪个版本的Python?并打印每个dict的repr(),以确保它们确实是相同的(不是只是它们比较相等)。我猜不到。例如,您可能正在使用python2,在一种情况下,char1char2是普通字符串,但在另一种情况下,它们是Unicode字符串。然后比较会说它们是相同的,但是repr()会显示出不同:

>>> d1 = {"a": 1}
>>> d2 = {u"a": 1}
>>> d1 == d2
True
>>> print repr(d1), repr(d2)
{'a': 1} {u'a': 1}

无论如何,在CPython中,绝对没有任何任何对象来自的内部“元数据”记录。你知道吗

编辑-要尝试的内容

很好地解决了这个问题!这正成为一种乐趣:-)我想让你试试。首先注释这行:

    scores = np.array(scores)

然后更改此行:

            score_dict[(alp[c1], alp[c2])] = scores[c1, c2]

收件人:

            score_dict[(alp[c1], alp[c2])] = scores[c1][c2]
                                                    ^^^^^^

当我这样做时,两个方法返回的时间基本相同。我不是numpy专家,但我的猜测是,您的“from file”代码对dict值使用的是机器原生的numpy整数类型,而且无论何时使用这些值,都有大量的开销将其转换为Python整数。你知道吗

或者也许不是-但这是我现在的猜测,我坚持;-)

相关问题 更多 >

    热门问题