Given
fncs = [func1, func2, ..., funcN]
args = [arg1, arg2, ..., argN]
How should one spell
results = map(lambda f,a: f(a), fncs, args)
in order to get the result most quickly ?
Unfortunately "apply" takes a tuple of arguments, and there is no
"funcall"[*] in Python.
[*] def funcall(fn, *args):
return fn(*args) 8 1648
On 12 Dec 2003 10:47:50 +0100, Jacek Generowicz <ja************ **@cern.ch> wrote: Given
fncs = [func1, func2, ..., funcN] args = [arg1, arg2, ..., argN]
How should one spell
results = map(lambda f,a: f(a), fncs, args)
in order to get the result most quickly ? Unfortunatel y "apply" takes a tuple of arguments, and there is no "funcall"[*] in Python.
[*] def funcall(fn, *args): return fn(*args) fncs = [lambda x,f='func_%s(%% s)'%i:f%x for i in xrange(4)] args = 'zero one two three'.split() map(lambda f,a: f(a), fncs, args)
['func_0(zero)', 'func_1(one)', 'func_2(two)', 'func_3(three)']
I'd probably try a list comprehension
[f(a) for f,a in zip(fncs,args)]
['func_0(zero)', 'func_1(one)', 'func_2(two)', 'func_3(three)']
Regards,
Bengt Richter
Jacek Generowicz <ja************ **@cern.ch> wrote in
news:ty******** *****@pcepsft00 1.cern.ch: Given
fncs = [func1, func2, ..., funcN] args = [arg1, arg2, ..., argN]
How should one spell
results = map(lambda f,a: f(a), fncs, args)
in order to get the result most quickly ?
Well, the way you wrote it isn't actually bad. The obvious alternative
(using a list comprehension) is slightly slower, but there isn't an awful
lot to choos between any of them, I suspect most of the time goes in the
actual f(a) function call. Any of these methods runs at about 300,000
function calls/second on my slow laptop. setup = """import itertools
def fn1(a): pass
fns = [fn1] * 100
args = [0] * 100
""" stmt1 = "result = [ f(a) for (f,a) in itertools.izip( fns, args) ]" stmt2 = "result = [ f(a) for (f,a) in zip(fns, args) ]" t = timeit.Timer(st mt1, setup) t.repeat(3,1000 0)
[3.5571303056673 855, 3.5537404893639 177, 3.5594278043718 077] t = timeit.Timer(st mt2, setup) t.repeat(3,1000 0)
[3.8932819674008 67, 3.8783479464568 7, 3.8829105375124 868] setup = """import itertools
def fn1(a): pass
fns = [fn1] * 1000
args = [0] * 1000
""" t = timeit.Timer(st mt1, setup) t.repeat(3,1000 )
[3.3503928571927 304, 3.3343195853104 248, 3.3495254285111 287] t = timeit.Timer(st mt2, setup) t.repeat(3,1000 )
[3.8062683944467 608, 3.7946001516952 492, 3.7881063096007 779] stmt3 = "results = map(lambda f,a: f(a), fns, args)" t = timeit.Timer(st mt3, setup) t.repeat(3,1000 )
[3.3275902384241 363, 3.3010907810909 202, 3.3174872784110 789]
The f(a) call is taking about half the time in any of these methods, so you
aren't going to get very much improvement whatever you do to the loop.
--
Duncan Booth du****@rcp.co.u k
int month(char *p){return(1248 64/((p[0]+p[1]-p[2]&0x1f)+1)%12 )["\5\x8\3"
"\6\7\xb\1\x9\x a\2\0\4"];} // Who said my code was obscure?
Jacek Generowicz wrote: Given
fncs = [func1, func2, ..., funcN] args = [arg1, arg2, ..., argN]
How should one spell
results = map(lambda f,a: f(a), fncs, args)
in order to get the result most quickly ?
I dont know if it will be faster (timeit might tell you this), but what
about something as simple stupid as:
results = []
nbfuns = len(fncs)
for i in range(len):
results.append( fncs[i](args[i]))
Note that you must have at least len(fncs) args.
HTH,
Bruno bo**@oz.net (Bengt Richter) writes: I'd probably try a list comprehension
>>> [f(a) for f,a in zip(fncs,args)]
['func_0(zero)', 'func_1(one)', 'func_2(two)', 'func_3(three)']
Yup, been there, done that, it's slower. (I guess I should have
mentioned that in the original post.)
Jacek Generowicz wrote: Given
fncs = [func1, func2, ..., funcN] args = [arg1, arg2, ..., argN]
How should one spell
results = map(lambda f,a: f(a), fncs, args)
in order to get the result most quickly ?
If you can afford to destroy the args list, a tiny speed gain might be in
for you (not verified):
for index, func in enumerate(fncs) :
args[index] = func[index]
Peter
"Jacek Generowicz" <ja************ **@cern.ch> wrote in message
news:ty******** *****@pcepsft00 1.cern.ch... Given
fncs = [func1, func2, ..., funcN] args = [arg1, arg2, ..., argN]
How should one spell
results = map(lambda f,a: f(a), fncs, args)
in order to get the result most quickly ? Unfortunately "apply" takes a tuple of arguments, and there is no "funcall"[*] in Python.
[*] def funcall(fn, *args): return fn(*args)
Building on a couple of other responses:
Untested code:
fncs = [func1, func2, ..., funcN]
args = [arg1, arg2, ..., argN]
results = []
for function, arguement in zip(fncs, args):
results.append( function(arguem ent))
Notice the use of zip() to put the two lists together.
I haven't timed it, but removing an extra layer of
function call has got to be faster. Likewise, zip
and tuple unpacking is most likely going to be
faster than indexing every time through the loop.
The append, on the other hand, might slow things
down a bit.
John Roth
"John Roth" <ne********@jhr othjr.com> wrote in
news:vt******** ****@news.super news.com: Building on a couple of other responses:
Untested code:
fncs = [func1, func2, ..., funcN] args = [arg1, arg2, ..., argN] results = [] for function, arguement in zip(fncs, args): results.append( function(arguem ent))
Notice the use of zip() to put the two lists together. I haven't timed it, but removing an extra layer of function call has got to be faster. Likewise, zip and tuple unpacking is most likely going to be faster than indexing every time through the loop. The append, on the other hand, might slow things down a bit.
Yes, getting rid of the append does indeed speed things up. On the same
system as I posted timings for the list comprehension, the fastest way I've
found so far is to get rid of the appends by preallocating the list, and to
go for the plain old simple technique of writing a for loop out explicitly.
Using 'enumerate' to avoid the lookup on one of the input lists is nearly
as fast, but not quite. setup = """import itertools
def fn1(a): pass
fns = [fn1] * 1000
args = [0] * 1000
"""
stmt1 = """result = args[:]
for i in range(len(fns)) :
result[i] = fns[i](args[i])
""" min(timeit.Time r(stmt1, setup).repeat(3 ,1000))
2.9747384094916 924 stmt3 = "results = map(lambda f,a: f(a), fns, args)" min(timeit.Time r(stmt3, setup).repeat(3 ,1000))
3.3257092731055 309
--
Duncan Booth du****@rcp.co.u k
int month(char *p){return(1248 64/((p[0]+p[1]-p[2]&0x1f)+1)%12 )["\5\x8\3"
"\6\7\xb\1\x9\x a\2\0\4"];} // Who said my code was obscure?
Peter Otten wrote: for index, func in enumerate(fncs) : args[index] = func[index]
Oops,
args[index] = func(args[index])
And it's much slower than result.append(f unc(args[index]), too :-(
Peter This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Vin |
last post by:
Is it possible to access a function defined in python shell from c-code?
For example if I have defined
>>> def c(x):
.... return x*x
....
and defined in some module foo
|
by: Ken Fine |
last post by:
I have a menu system that has nodes that can be opened or closed. In an
effort to make my code more manageable, I programmed a little widget tonight
that keeps track of the open/active item and automatically builds
querystrings for my redirect URLS. The code for this follows. It defines an
ASP Dictionary object, and key/value pairs for each, and builds appropriate
querystrings based on comparison with a status variable.
The way it works...
|
by: Mike Darrett |
last post by:
Hello,
First off, I'm a C++ newbie, so please turn your flame guns off. ;)
I'm wondering if this is the correct way to have a function return a
vector:
vector<int> GetVecData()
{
vector<int> ret;
|
by: Materialised |
last post by:
I am writing a simple function to initialise 3 variables to pesudo
random numbers.
I have a function which is as follows
int randomise( int x, int y, intz)
{
srand((unsigned)time(NULL));
x = rand();
y = rand();
|
by: Jens.Toerring |
last post by:
Hi,
I have a possibly rather stupid question about the order of evaluation
in a statement like this:
result = foo( x ) - bar( y );
How can I make 100% sure that foo(x) is evaluated before bar(y), where
foo() and bar() can be either functions or macros? I got into problems
with this when writing some hardware-related stuff where foo() and bar()
| |
by: pembed2003 |
last post by:
Hi all,
I need to write a function to search and replace part of a char*
passed in to the function. I came up with the following:
char* search_and_replace(char* source,char search,char* replace){
char* result;
size_t l = strlen(source), r = strlen(replace), i;
int number_of_replaces = 0;
for(i = 0; i < l; i++){
if(source == search)
|
by: Ken Varn |
last post by:
I want to be able to determine my current line, file, and function in my C#
application. I know that C++ has the __LINE__, __FUNCTION__, and __FILE___
macros for getting this, but I cannot find a C# equivalent. Any ideas?
--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.
varnk@diebold.com
|
by: the_real_remi |
last post by:
Hi,
I'm writing a code that has to be as efficient as possible both in terms of
memory use and execution speed. I'll have to pass a class instance (which is
an 'intelligent' array) to a function (multiple times). Taking this into
consideration, should I pass it as a pointer or reference? Is there any book
or site that teaches stuff like this (optimizing code for performance)?
thanks for any help
|
by: bluejack |
last post by:
Ahoy... before I go off scouring particular platforms for specialized
answers, I thought I would see if there is a portable C answer to this
question:
I want a function pointer that, when called, can be a genuine no-op.
Consider:
typedef int(*polymorphic_func)(int param);
|
by: thinktwice |
last post by:
i have just made a test project :(win32 console)
//file : func.h
#ifndef _FUNC_H_
#define _FUNC_H_
void func1()
{
return;
};
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |