Either use standard python "def" functions rather than cdef functions,
or index into an array of function pointers with enums.
- Robert
Apologies for my ignorance, but could you show an example of an array of
funtion pointers and their use with enums?
~Ethan~
If you don't know what it means, you probably don't want it :) Is
there any reason you're not using def functions?
At this point I am still gathering knowledge. I hope to write a Cython
version of my dbf module at some point.
~Ethan~
I'll second Mark's point.
> At this point I am still gathering knowledge. I hope to write a Cython
> version of my dbf module at some point.
Best of luck; let us know if you run into any more blockers.
- Robert
On Mon, Feb 6, 2012 at 3:33 PM, Ethan Furman <et...@stoneleaf.us> wrote:I'll second Mark's point.
> mark florisson wrote:
>>
>> On 4 February 2012 20:53, Ethan Furman wrote:
>>>
>>> Robert Bradshaw wrote:
>>>>
>>>> On Tue, Jan 31, 2012 at 1:12 PM, paomo wrote:
>>>>>
>>>>> cdef f1():
>>>>> ...
>>>>> cdef f2():
>>>>> ...
>>>>> my_functions = {'case_1' : f1,
>>>>> 'case_2' : f2} # this won't compile
>>>>> my_functions['case_1']()
>>>>
>>>>
>>>> Either use standard python "def" functions rather than cdef functions,
>>>> or index into an array of function pointers with enums.
>>>
>>>
>>> Apologies for my ignorance, but could you show an example of an array of
>>> funtion pointers and their use with enums?
>>
>>
>> If you don't know what it means, you probably don't want it :) Is
>> there any reason you're not using def functions?
OK, looks like you know what you're doing :). Yes, this is what I meant.
> The cython function that uses these functions looks like:
>
> def cy_calc_fptr(double x, int selected_func):
> ...
> theResult = funcList[selected_func](x)
> ...
>
> It compiles, and I get the expected ouput. However this is still 3 orders of
> magnitude slower than the following ugly hard coded implementation:
>
> def cy_ref_calc2(double x, double y, int function_id):
> ...
> if function_id == 0:
> theResult = basefunc0(x)
> else:
> theResult = basefunc1(x)
> ...
>
> The cython -a results show this as being "direct" C code; so it would seem
> that using the function pointer causes a large overheard even in C, which is
> unexpected. Am I missing something or is that just the way it works?
I think that's just the way it is. Function pointers require an extra
level of indirection (as well as missing out on a number of
optimizations that could be performed after inlining such a short
function, which is probably the larger issue at play here).
> The cython function that uses these functions looks like:
>
> def cy_calc_fptr(double x, int selected_func):
> ...
> theResult = funcList[selected_func](x)
> ...
>
> It compiles, and I get the expected ouput. However this is still 3 orders of
> magnitude slower than the following ugly hard coded implementation:
>
> def cy_ref_calc2(double x, double y, int function_id):
> ...
> if function_id == 0:
> theResult = basefunc0(x)
> else:
> theResult = basefunc1(x)
> ...
>
> The cython -a results show this as being "direct" C code; so it would seem
> that using the function pointer causes a large overheard even in C, which is
> unexpected. Am I missing something or is that just the way it works?
I think that's just the way it is. Function pointers require an extra
level of indirection (as well as missing out on a number of
optimizations that could be performed after inlining such a short
function, which is probably the larger issue at play here).
Was that with a cdef or cpdef method? It's just using a virtual
function table, so I'd hope there isn't additional overhead. (Well, I
suppose it's two defererences rather than one, but unless your
function is trivial that should be in the noise...)
> Sorry for the confusion!
No problem. Sounds like you're making a lot of progress (and it's good
to have this kind of thread documented for future users as well).
- Robert