English 中文(简体)
将(-1,0)转换成图的最快方式?
原标题:Fastest way to convert (-1,0) into tuple(-1, 0)?
  • 时间:2009-10-24 20:19:14
  •  标签:

我看一看一幅巨大的镜子,正从一个方案返回。 回归的一个实例是:

( (-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
问题回答

我不建议你完全使用电子数据。 这是缓慢和不安全的。 你可以这样做:

result = map(lambda a: tuple(map(int, a[1:-1].split( , ))), s)

这些数字表明自己:

timeit.Timer("map(lambda a: tuple(map(int, a[1:-1].split( , ))), s)", "s = ( (-1,0) ,  (1,0) ,  (2,0) ,  (3,0) ,  (4,0) ,  (5,0) ,  (6,0) )").timeit(100000)

1.8787779808044434

timeit.Timer("map(eval, s)", "s = ( (-1,0) ,  (1,0) ,  (2,0) ,  (3,0) ,  (4,0) ,  (5,0) ,  (6,0) )").timeit(100000)

11.571426868438721
map(eval, tuples)

这为其中一只 t子 correct然正确的例子赢得了肯定。 为此,我建议:

def do(tup):
    try: return eval(tup)
    except: return None

map(do, tuples)

两种快速测试方法:

>>> tuples = ["(1,0)"] * 1000000

>>> # map eval
>>> st = time.time(); parsed = map(eval, tuples); print "%.2f s" % (time.time() - st)
16.02 s

>>> # map do
>>> >>> st = time.time(); parsed = map(do, tuples); print "%.2f s" % (time.time() - st)
18.46 s

不包括bad的1 000 000个胶囊(但不是great)。 间接费用大概使用<代码>eval。 然而,这是你之后做些什么的最容易的方法。

使用清单理解而不是<代码><>>><>>>/代码”的答案与我的尝试/例外情形(本身利益)一样缓慢:

>>> st = time.time(); parsed = [eval(t) for t in tuples]; print "%.2f s" % (time.time() - st)
18.13 s

尽管如此,我冒着过早优化的风险,这里的工作总是很缓慢。 你们期待多少个les子?

如果你知道这种格式,我确实说教。 速度大于电子数据。

>>> tuples = ["(1,0)"] * 1000000
>>> import time
>>> st = time.time(); parsed = map(eval, tuples); print "%.2f s" % (time.time() - st)
32.71 s
>>> def parse(s) :
...   return s[1:-1].split(",")
...
>>> parse("(1,0)")
[ 1 ,  0 ]
>>> st = time.time(); parsed = map(parse, tuples); print "%.2f s" % (time.time() - st)
5.05 s

如果需要的话,

>>> def parse(s) :
...   return map(int, s[1:-1].split(","))
...
>>> parse("(1,0)")
[1, 0]
>>> st = time.time(); parsed = map(parse, tuples); print "%.2f s" % (time.time() - st)
9.62 s

我的计算机比Nadias慢,但速度更快。

>>> timeit.Timer(
    "list((int(a),int(c)) for a,b,c in (x[1:-1].partition( , ) for x in s))", 
    "s = ( (-1,0) ,  (1,0) ,  (2,0) ,  (3,0) ,  (4,0) ,  (5,0) ,  (6,0) )").timeit(100000)
3.2250211238861084

页: 1

>>> timeit.Timer(
    "map(lambda a: tuple(map(int, a[1:-1].split( , ))), s)", 
    "s = ( (-1,0) ,  (1,0) ,  (2,0) ,  (3,0) ,  (4,0) ,  (5,0) ,  (6,0) )").timeit(100000)
3.8979239463806152

采用清单核对表的速度仍然更快

>>> timeit.Timer(
    "[(int(a),int(c)) for a,b,c in (x[1:-1].partition( , ) for x in s)]", 
    "s = ( (-1,0) ,  (1,0) ,  (2,0) ,  (3,0) ,  (4,0) ,  (5,0) ,  (6,0) )").timeit(100000)
2.452484130859375

如果你确定投入是充分的:

tuples = ( (-1,0) ,  (1,0) ,  (2,0) ,  (3,0) ,  (4,0) ,  (5,0) ,  (6,0) )
result = [eval(t) for t in tuples]

你们可以坐下来,与YAPPS一道迅速运作。

你们只能使用Yaml或json,把它 par成 you子。

import ast

list_of_tuples = map(ast.literal_eval, tuple_of_strings)




相关问题
热门标签