How is that function pointer better than if-else or switch case? Is it because function pointer helps callback functions and thus promotes asynchronous implementation?
It's better if the functions are not known beforehand. How would you design the C standard library qsort() without using a function pointer?
It's not "better", it's different. Yes, the main purpose of function pointer is to provide callback functionality. No, it is not directly related to asynchronism.
Check this article for more information on function pointers.
According to Nigel Jones, it improves code readability, among others
http://embeddedgurus.com/stack-overflow/2010/04/efficient-c-tip-12-be-wary-of-switch-statements/
He even provides some examples:
http://www.rmbconsulting.us/Publications/jump-Tables.pdf
Function pointers result in a better design of your code. Consider when you have an option of calling one function out of fifty functions. How massive would the switch case be? However, you could easily map all the function pointers as per their id, and call the appropriate function from the map using the "id". The code would be easily maintained and look neat.
There are other benefits and powers of using function pointer.
For a good explanation, look at the tutorial here
Related
I have seen other questions related to this topic but any of them has an explanation on how the compiler deal with these functions and how they are dispatched, if they behave like a inline function or what?
Basically I would like to know the performance difference between using global functions and class/static functions.
Note: I am not asking if it is a good practice or not, just which option has better performance.
According to this link, the default is to inline them sometimes. Compilers have gotten pretty good at figuring out when something can be optimized but if you want to prevent or force an inline, you can do it with the #inline attribute.
I'm not sure how this affects performance, but if I had to guess I'd say the global function might be slightly faster than the class function unless the class is marked as final or the function is marked static.
Is there a guideline or recommendation about how to pass object to functions? Is it recommended to always pass the full object to a function:
calculateSomething(car1, car2, aircraft)
Or is it better to only pass the properties that are really needed to the function?
calculateSomething(car1.speed, car1.length, car2.speed, aircraft.height)
The first approach seems to be more convenient, especially when the function requires many more properties. However, my intuition tells me that the second approach is more computationally efficient as the function does not has to handle the full objects.
Is there a general programming advice for this or is it for every function a trade-off between readability and speed?
Never pass the properties directly. Because that breaks the principles of Object orientated programming, (Encapsulation) specially if it will involve making changes to the properties.
Always use getters and setters to make changes to the object properties.
I have heard this term tossed around, can anybody explain what it means and what problem it solves. Also where does this originate from.
Simplified it's when you pass a function to another function, and that other function call you back using the function you passed.
It's useful for asynchronous programming, when events can happen at any time. Or for generic handling of certain functionality to make some algorithms more generic (for example in C++ you can pass a comparison function to the std::sort function, and your comparison function will be used to compare two items when sorting).
Hello everyone help me please!!!!
What can use to avoid the msgSend function overhead?
maybe answer is IMP, but I not sure.
You could simply inline the function to avoid any function call overhead. Then it would be faster than even a C function! But before you start down this path - are you certain this level of optimisation is warranted? You are more likely to get a better payoff by optimizing the algorithm.
The use of IMP is very rarely required. The method dispatching in Objective-C (especially in the 64-bit runtime) has been very heavily optimised, and exploits many tricks for speed.
What profiling have you done that tells you that method dispatching is the cause of your performance issue? I suggest first you examine the algorithm to see first of all where the most expensive operations are, and see if there is a more efficient way to implement it.
To answer your question, a quick search finds some directly relevant questions similar to yours right here on SO, with some great and detailed answers:
Objective-C optimization
Objective-C and use of SEL/IMP
What are the differences between normal function and function pointer.
One which I know is in case you are using a library in your software stack which
gives you only function pointers then you can fill in the pointers for
use later.
Calling a function through a function pointer means the call cannot be inlined; in certain cases, this can result in quite a performance penalty. (For example, C's qsort() going through a function pointer for each compare, vs. C++'s sort() being able to inline the comparison.)
Declaring a function pointer requires a non-trivial syntax that is not as commonly used as other parts of the language, resulting in a "mental speed-bump" for most when reading the source. It is usually typedef'ed for that reason.
A function pointer is just a pointer to a normal function. It can be passed around like any other pointer, and can be invoked somewhere other than where it was created.