Efficient way to call many functions in Python

I have a set of functions:

functions=set(...) 

All functions require one parameter x.

What is the most efficient way in python to do something similar to:

 for function in functions: function(x) 
0
source share
4 answers

The code you specify is

 for function in functions: function(x) 

... does nothing with the result of calling function(x) . If this is true, that is, these functions are called for their side effects, then there is no longer an alternative to pythons. Just leave your code as it is. & dagger; Point to go home here in particular

Avoid features with side effects in the list settings.

As for efficiency: I expect that using something else instead of your simple loop will not improve the runtime. If in doubt, use timeit . For example, the following tests seem to indicate that a regular for-loop is faster than a list comprehension. (I would not want to draw any general conclusions from this test, I thought):

 >>> timeit.Timer('[f(20) for f in functions]', 'functions = [lambda n: i * n for i in range(100)]').repeat() [44.727972984313965, 44.752119779586792, 44.577917814254761] >>> timeit.Timer('for f in functions: f(20)', 'functions = [lambda n: i * n for i in range(100)]').repeat() [40.320928812026978, 40.491761207580566, 40.303879022598267] 

But then again, even if these tests would indicate that rethinking lists is faster, it remains that you should not use them when side effects are involved, for readability.


& dagger; : Well, I would write for f in functions , so the difference between function and functions more pronounced. But this is not said.

+7
source

If you need a way out, a list comprehension will be implemented.

 [func(x) for func in functions] 
+1
source

I somewhat doubt how much this will affect the overall running time of your program, but I think you could do something like this:

 [func(x) for func in functions] 

The downside is that you create a new list that you drop immediately, but it should be a little faster than just for the loop.

In any case, make sure you comment on your code to confirm that this is really a bottleneck that you need to take care of.

0
source

Edit: I reinstalled the test using timeit

My new test code:

 import timeit def func(i): return i; a = b = c = d = e = f = func functions = [a, b, c, d, e, f] timer = timeit.Timer("[f(2) for f in functions]", "from __main__ import functions") print (timer.repeat()) timer = timeit.Timer("map(lambda f: f(2), functions)", "from __main__ import functions") print (timer.repeat()) timer = timeit.Timer("for f in functions: f(2)", "from __main__ import functions") print (timer.repeat()) 

Here are the results of this time.

 testing list comprehension [1.7169530391693115, 1.7683839797973633, 1.7840299606323242] testing map(f, l) [2.5285000801086426, 2.5957231521606445, 2.6551258563995361] testing plain loop [1.1665718555450439, 1.1711149215698242, 1.1652190685272217] 

My time-based time, time.time (), is pretty much related to this testing, simple loops seem to be the most efficient.

0
source

All Articles