If you use a card with a built-in function, it may be slightly faster than the LC:
>>> strs = " ".join(str(x) for x in xrange(10**5)) >>> %timeit [int(x) for x in strs.split()] 1 loops, best of 3: 111 ms per loop >>> %timeit map(int, strs.split()) 1 loops, best of 3: 105 ms per loop
With custom function:
>>> def func(x): ... return int(x) >>> %timeit map(func, strs.split()) 1 loops, best of 3: 129 ms per loop >>> %timeit [func(x) for x in strs.split()] 1 loops, best of 3: 128 ms per loop
Python 3.3.1 comparison:
>>> strs = " ".join([str(x) for x in range(10**5)]) >>> %timeit list(map(int, strs.split())) 10 loops, best of 3: 59 ms per loop >>> %timeit [int(x) for x in strs.split()] 10 loops, best of 3: 79.2 ms per loop >>> def func(x): return int(x) ... >>> %timeit list(map(func, strs.split())) 10 loops, best of 3: 94.6 ms per loop >>> %timeit [func(x) for x in strs.split()] 1 loops, best of 3: 92 ms per loop
From Python Performance Tips :
The only limitation is that the "body of the loop" of the card must be a challenge function. Besides the syntactic benefits of understanding lists, they are often just as fast or faster than using the map equivalently.
Ashwini chaudhary
source share