我看一看一幅巨大的镜子,正从一个方案返回。 回归的一个实例是:
( (-1,0) , (1,0) , (2,0) , (3,0) , (4,0) , (5,0) , (6,0) )
页: 1 任何东西方都感觉到,正以相对“缓慢”的方式这样做。 正如我刚才提到的那样,这些清单可能很庞大,因此很快将受到高度赞赏!
增 编
edit one Alright, so its seeming that eval is a slower method of doing this. But so far i ve got 4 methods tested, thanks for any comments and submissions! :)
此外,有人询问我的les体大小。 它将在任何地方进行,从少数几个国家到希望不超过几个百万。 速度不是“to”,而是足够大,是一个重要因素。 我在这里不是为了微观激励,而只是学习任何可能不了解的新门槛。 例如,(eval)常常是忘记的,尽管在这种情况下它似乎做得不错。
edit two I also wanted to note that the string format shouldn t change. So no need to check the format. Also, this is an embedded Python v2.6.2, so anything requiring 2.6 is fine. 3.0 on the other hand, not so much ;)
感谢所有投入:
edit 3 Yet another note. I noticed i had been returning code that didn t result in a "tuple", this is ok, and sorry if anyone thought the end result "had" to be a tuple. Something of like format is fine.
import timeit
test_tuple = ( (-1,0) , (1,0) , (2,0) , (3,0) , (4,0) , (5,0) , (6,0) , (7,0) ,)
def timeit_a():
def convert_tup_strings(tup_string):
first_int, last_int = tup_string[1:-1].split( , )
return (int(first_int), int(last_int))
return map(convert_tup_strings, test_tuple)
def timeit_a_1():
def convert_tup_strings(tup_string):
return map(int, tup_string[1:-1].split( , ))
return map(convert_tup_strings, test_tuple)
def timeit_b():
converted = []
for tup_string in test_tuple:
first_int, last_int = tup_string[1:-1].split( , )
converted.append((int(first_int), int(last_int)))
return converted
def timeit_b_1():
converted = []
for tup_string in test_tuple:
converted.append(map(int, tup_string[1:-1].split( , )))
return converted
def timeit_c():
return [eval(t) for t in test_tuple]
def timeit_d():
return map(eval, test_tuple)
def timeit_e():
return map(lambda a: tuple(map(int, a[1:-1].split( , ))), test_tuple)
print Timeit timeit_a: %s % timeit.timeit(timeit_a)
print Timeit timeit_a_1: %s % timeit.timeit(timeit_a_1)
print Timeit timeit_b: %s % timeit.timeit(timeit_b)
print Timeit timeit_b_1: %s % timeit.timeit(timeit_b_1)
print Timeit timeit_c: %s % timeit.timeit(timeit_c)
print Timeit timeit_d: %s % timeit.timeit(timeit_d)
print Timeit timeit_e: %s % timeit.timeit(timeit_e)
成果:
Timeit timeit_a: 15.8954099772
Timeit timeit_a_1: 18.5484214589
Timeit timeit_b: 15.3137666465
Timeit timeit_b_1: 17.8405181116
Timeit timeit_c: 91.9587832802
Timeit timeit_d: 89.8858157489
Timeit timeit_e: 20.1564312947