Map vs For Loop

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.

In [1]:
def convert_for1(data):
    output = []
    for line in data:
        x,y = line.split(',')
    return output

def convert_for2(data):
    output = []
    for line in data:
        x,y = line.split(',')
    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.

In [2]:
text = """ 1,2
data = text.splitlines()

Checking to see that they return the same thing...

In [3]:
[[1.0, 2.0], [2.0, 3.0], [3.0, 4.0]]
In [4]:
[[[1.0, 2.0]], [[2.0, 3.0]], [[3.0, 4.0]]]
In [5]:
[[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?

In [6]:
%timeit convert_for1(data)
100000 loops, best of 3: 5.63 us per loop

In [7]:
%timeit convert_for2(data)
100000 loops, best of 3: 5.66 us per loop

In [8]:
%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.

Back to top