[Solved] Performance gain using anonymous functions? [closed]


The question itself is easily answered, no there is no performance gain in using anonymous functions in Python. There is a good chance you are actually making it slower.

A simple timeit tests on trivial functions show that there is no real difference between the two. We take these two functions

def test(message):
    return message + message

testanon = lambda message: message + message

and then use the timeit module to test their execution speed:

>>> timeit.repeat("test('test')", setup="from __main__ import test")
[0.16360807418823242, 0.1602180004119873, 0.15763211250305176]
>>> timeit.repeat("testanon('test')", setup="from __main__ import testanon")
[0.15949010848999023, 0.15913081169128418, 0.17438983917236328]

As is visible, there is no real big performance increase worth actually considering, your performance problem most likely lies somewhere else.

solved Performance gain using anonymous functions? [closed]