我正在做序列比对,遇到了一个与我的字典数据结构起源相关的相当神秘的计时问题。
基本上,我有函数 alignment(s1, s2, scores)
它接受两个字符串 s1 和 s2,以及每对可能的 20 个氨基酸和一个空位“-”的评分矩阵(作为 python 字典)。所以 scores
有 440 个键(char1、char2),具有整数值。
神秘之处在于:如果我从文本文件(称之为 scores1)中读取 scores
并运行
对齐(s1,s2,scores1)
对于一些 1000-ish 长字符串 s1,s2 氨基酸我得到以下时间(使用 cProfile 并且不显示函数输出):
11.796 秒内调用 2537776 次函数
现在,如果我在我的文件中创建完全相同的字典(称之为 scores2)并运行
对齐(s1,s2,scores2)
我得到了相同的输出结果,但时间减少了 3 倍:
在 4.263 秒内调用了 2537776 次函数
两种情况下的输出是相同的,只是时间不同。
运行 print scores1 == scores2
结果为 True
,因此它们包含相同的信息。
我验证了使用访问字典的任意函数(而不是对齐)
在这两种情况下,多次产生相同的 3 倍时序差异。
一定有一些元数据与字典的来源相关,这会减慢我的功能(当来自文件时),即使在这两种情况下我实际上都在文件中读取。
我尝试通过 scores1 = dict(scores1)
等为每个对象创建一个新的 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_scores.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
最佳答案
哪个版本的 Python?并打印每个字典的 repr()
以确保它们确实相同(而不是只是它们比较相等)。猜不出来。例如,也许您正在使用 Python 2,并且在一种情况下您的 char1
和 char2
是纯字符串,但在另一种情况下它们是 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
专家,但我的猜测是您的“来自文件”代码使用机器 native numpy
整数类型作为 dict 值,并且有每当使用这些值时,将它们转换为 Python 整数的开销很大。
也可能不是 - 但这是我现在的猜测,我会坚持下去 ;-)
关于python dict时间之谜,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20929805/