Michael - 1 year ago 44

Python Question

I do some speed tests for operations on vectors/lists. Suprisingly,

`map`

`filter`

`numpy`

`n = 10000000`

a = np.random.rand(n)

b = np.random.rand(n)

c = a + b # time = 0.07 s

d = a[a < 0.3] # time = 0.09 s

a = [random.random() for x in range(0, n, 1)]

b = [random.random() for x in range(0, n, 1)]

c = map(lambda x, y: x + y, a, b) # time = 0.006s

d = filter(lambda e: e < 0.3, a) # time = 0.001s

Is it really possible that

`map`

`filter`

`numpy`

`import numpy as np`

import time

import random

class StopWatch:

def __init__(self, str):

self.str = str

self.t = time.time()

def stop(self):

t = time.time()

print("time = " + str(t - self.t) + " s for " + self.str)

n = 10000000

a = np.random.rand(n)

b = np.random.rand(n)

sw = StopWatch('numpy')

c = a + b

sw.stop()

sw = StopWatch('numpy')

d = a[a < 0.3]

sw.stop()

a = [random.random() for x in range(0, n, 1)]

b = [random.random() for x in range(0, n, 1)]

sw = StopWatch('list')

c = map(lambda x, y: x + y, a, b)

sw.stop()

sw = StopWatch('list')

d = filter(lambda e: e < 0.3, a)

sw.stop()

If my measurements are correct, WHY is it that much faster?

Answer Source

My guess is that `c = map(lambda x, y: x + y, a, b)`

is actually not calculated. In python 3, `map`

and `filter`

are evaluated lazy, and therefore not before they have to be evaluated.

You can verify this by adding a `list(c)`

before you stop the timer, though this might affect the time a little more for the list creation.