I've been looking at a lot of mapping functions today and I thought I would see how maps fare against a simple for loop.
def convert_for1(data):
output = []
for line in data:
x,y = line.split(',')
output+=[[float(x),float(y)]]
return output
def convert_for2(data):
output = []
for line in data:
x,y = line.split(',')
output.append([[float(x),float(y)]])
return output
def convert_map(data):
return [list(map(float, x.split(','))) for x in data]
Well I have to admit that the code looks a lot cleaner using the mapping function.
text = """ 1,2
2,3
3,4"""
data = text.splitlines()
Checking to see that they return the same thing...
convert_for1(data)
[[1.0, 2.0], [2.0, 3.0], [3.0, 4.0]]
convert_for2(data)
[[[1.0, 2.0]], [[2.0, 3.0]], [[3.0, 4.0]]]
convert_map(data)
[[1.0, 2.0], [2.0, 3.0], [3.0, 4.0]]
Let check on the speed now. Does few lines of code mean a faster function?
%timeit convert_for1(data)
100000 loops, best of 3: 5.63 us per loop
%timeit convert_for2(data)
100000 loops, best of 3: 5.66 us per loop
%timeit convert_map(data)
100000 loops, best of 3: 8.79 us per loop
Well I guess that settles it for me. I find the convert_for1 loop the most readable, and now I see that it is just faster too now. That is the approach I'm using from now on.
Oh, I know that using numba or cython would be faster, but this is the approach I'll use unless I am looking for real speed.