English 中文(简体)
Efficient way of calling set of functions in Python
原标题:

I have a set of functions:

functions=set(...)

All the functions need one parameter x.

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

for function in functions:
   function(x)
最佳回答

The code you give,

for function in functions:
    function(x)

...does not appear to do anything with the result of calling function(x). If that is indeed so, meaning that these functions are called for their side-effects, then there is no more pythonic alternative. Just leave your code as it is. The point to take home here, specifically, is

                               Avoid functions with side-effects in list-comprehensions.

As for efficiency: I expect that using anything else instead of your simple loop will not improve runtime. When 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 be reluctant to draw any general conclusions from this test, 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 again, even if these tests would have indicated that list-comprehensions are faster, the point remains that you should not use them when side-effects are involved, for readability s sake.


  : Well, I d write for f in functions, so that the difference beteen function and functions is more pronounced. But that s not what this question is about.

问题回答

If you need the output, a list comprehension would work.

[func(x) for func in functions]

I m somewhat doubtful of how much of an impact this will have on the total running time of your program, but I guess you could do something like this:

[func(x) for func in functions]

The downside is that you will create a new list that you immediatly toss away, but it should be slightly faster than just the for-loop.

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

Edit: I redid 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 is the results from this timing.

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 original, time.time() based timings are pretty much inline with this testing, plain for loops seem to be the most efficient.





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签