I've got a huge tuple of strings, which are being returned from a program. An example tuple being returned might look like this:
('(-1,0)', '(1,0)', '(2,0)', '(3,0)', '(4,0)', '(5,0)', '(6,0)')
I can convert these strings to real tuples (with integers inside), but i am hoping someone knows a nice trick to speed this up. Anything i've come up with feels like i am doing it a relatively "slow" way. And as i have mentioned, these lists can be big, so a fast way would be much appreciated!
Thanks
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! :)
Also, someone asked on the size of my tuples. It will range anywhere from a few, to hopefully no more than a few million. Not "too" big, but big enough that speed is an important factor. I'm not here to micro-optimize, just learn any new nifty tricks i might not be aware of. Eg, eval() is something i often forget about, even though it doesn't seem to do so well in this case.
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 ;)
Looking great guys, again, thanks for all the input :)
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)
Results in:
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