function-definition


Should I define my Cython function using def, cdef, or cpdef for optimal performance?

那年仲夏 提交于 2020-01-01 10:53:07
问题 How can I know whether to use def, cdef or cpdef when defining a Cython function, assuming I want optimal performance? 回答1: If you want optimal performance, you should know that as mentioned in this answer to a related question: Once the function has been called there is no difference in the speed that the code inside a cdef and a def function runs at. So for optimal Cython performance you should always statically type all arguments and variables , and intuitively you would then be tempted to

What does -> mean in Python function definitions?

半世苍凉 提交于 2019-12-16 20:04:40
问题 I've recently noticed something interesting when looking at Python 3.3 grammar specification: funcdef: 'def' NAME parameters ['->' test] ':' suite The optional 'arrow' block was absent in Python 2 and I couldn't find any information regarding its meaning in Python 3. It turns out this is correct Python and it's accepted by the interpreter: def f(x) -> 123: return x I thought that this might be some kind of a precondition syntax, but: I cannot test x here, at it is still undefined, No matter

How to hash a class or function definition?

99封情书 提交于 2019-12-12 11:31:27
问题 Background When experimenting with machine learning, I often reuse models trained previously, by means of pickling/unpickling. However, when working on the feature-extraction part, it's a challenge not to confuse different models. Therefore, I want to add a check that ensures that the model was trained using exactly the same feature-extraction procedure as the test data. Problem My idea was the following: Along with the model, I'd include in the pickle dump a hash value which fingerprints the

Nested `constexpr` function calls before definition in a constant-expression context

微笑、不失礼 提交于 2019-12-10 16:46:50
问题 From what I gather from this answer, a constexpr function's result is not a constant-expression if the function has not been declared yet. What surprises me is the following code snippet : constexpr int f(); constexpr int g() { return f(); } constexpr int f() { return 42; } int main() { constexpr int i = g(); return i; } This compiles without trouble and works. Moving f 's definition past main triggers error: 'constexpr int f()' used before its definition , as I would expect. I presume that

behaviour of implicit function declaration

孤街浪徒 提交于 2019-12-08 14:12:13
问题 I know it is wrong to use a function without prototype. But when I was fiddling around, I came across this strange and conflicting behavior. test1 #include <stdio.h> #include <limits.h> void main(){ char c='\0'; float f=0.0; xof(c,f);/* at this point implicit function declaration is generated as int xof(int ,double ); */ } int xof(char c,float f) { printf("%d %f\n", c,f); } Implicit function declaration would be int xof(int ,double ); error is variablename.c:8:5: error: conflicting types for

Should I define my Cython function using def, cdef, or cpdef for optimal performance?

寵の児 提交于 2019-12-04 07:32:31
How can I know whether to use def, cdef or cpdef when defining a Cython function, assuming I want optimal performance? If you want optimal performance, you should know that as mentioned in this answer to a related question: Once the function has been called there is no difference in the speed that the code inside a cdef and a def function runs at. So for optimal Cython performance you should always statically type all arguments and variables , and intuitively you would then be tempted to use cdef , but there are some caveats for which I constructed the flowchart below (also based on previously

Why is Taking the Address of a Function That is Declared Only Working?

痴心易碎 提交于 2019-12-02 20:06:31
问题 I've asked a question here about whether taking the address of a function forces the compilation of said function specifically with regard to Substitution-Failure-Is-Not-An-Error. The most direct answer to this can be found here: Informally, an object is odr-used if its address is taken, or a reference is bound to it, and a function is odr-used if a function call to it is made or its address is taken. If an object or a function is odr-used, its definition must exist somewhere in the program;

Why is Taking the Address of a Function That is Declared Only Working?

半世苍凉 提交于 2019-12-02 09:15:39
I've asked a question here about whether taking the address of a function forces the compilation of said function specifically with regard to Substitution-Failure-Is-Not-An-Error. The most direct answer to this can be found here : Informally, an object is odr-used if its address is taken, or a reference is bound to it, and a function is odr-used if a function call to it is made or its address is taken. If an object or a function is odr-used, its definition must exist somewhere in the program; a violation of that is a link-time error. But all the compilers I've tested show this as perfectly

Alternative (K&R) C syntax for function declaration versus prototypes

北慕城南 提交于 2019-11-25 21:56:05
问题 What is useful about this C syntax — using \'K&R\' style function declarations? int func (p, p2) void* p; int p2; { return 0; } I was able to write this in Visual Studios 2010beta // yes, the arguments are flipped void f() { void* v = 0; func(5, v); } I don\'t understand. What\'s the point of this syntax? I can write: int func (p, p2) int p2; { return 0; } // and write int func (p, p2) { return 0; } The only thing it seems to specify is how many parameters it uses and the return type. I guess

工具导航Map