Function pointer to inline function ?


 
Thread Tools Search this Thread
Top Forums Programming Function pointer to inline function ?
# 8  
Old 09-28-2009
Quote:
Originally Posted by emitrax
The point is that the function is called _VERY_ often, so the more is optimized, the better.
There's almost nothing there to optimize. At best the difference will be minimal, at worst you'll make it slower. If the compiler can inline htons(), it'll become perhaps one branch-instruction optionally skipping one to three instructions that do the byte swap. That's likely fewer instructions than it takes to call a function, let alone run the code inside it. (and if it's not inlining, that's something you could do...) Function calls can be expensive.
# 9  
Old 09-30-2009
Quote:
Originally Posted by jim mcnamara
General tips:

If you have already not done so - try profiling your code.

Algorithm changes usually provide far better optimization than tweaks like function pointers. This is the reason for seemingly odd algorithms like Duff's machine. And threading.
Do you have a _VERY_ good tutorial about profiling with gprof ? I'm actually having
some trouble finding one. I found many, but none that satisfies me.
I'm particularly interested in profiling library code (shared).

Quote:
Originally Posted by jim mcnamara
Turn on optimization for your compiler - things like loop unrolling may provide a lot of speed increase.
Hmm... optimization sometimes lead to very hard to find bugs, at least in my little experience. So I'd rather leave that as my last option.

Thanks again!
S.

---------- Post updated at 09:29 AM ---------- Previous update was at 09:28 AM ----------

Quote:
Originally Posted by Corona688
There's almost nothing there to optimize. At best the difference will be minimal, at worst you'll make it slower. If the compiler can inline htons(), it'll become perhaps one branch-instruction optionally skipping one to three instructions that do the byte swap. That's likely fewer instructions than it takes to call a function, let alone run the code inside it. (and if it's not inlining, that's something you could do...) Function calls can be expensive.
Thanks for the explanation!

Regards,
S.
# 10  
Old 09-30-2009
Quote:
Originally Posted by emitrax
Hmm... optimization sometimes lead to very hard to find bugs, at least in my little experience. So I'd rather leave that as my last option.
Optimization can cause errors in programs with subtle bugs. Places where accidental writing to stack or incidental return values hadn't mattered before, suddenly may. Proper programming practices will circumvent this, and the benefits of optimization can be very good.
# 11  
Old 09-30-2009
Quote:
I'm particularly interested in profiling library code (shared).
Try a static link - that is one easy way to profile shared routines.
However try these as base assumptions:

most of the problems you have are yours, not library code
algorithms and proper program design give the best return on optimization by far
code tricks result in very minor gains

Do you know about aio (asynchronous I/O)? try reading up on that. It is often used in high demand high volume I/O design (disk). I think it is still not fully supported for sockets in Linux. I don't know for sure.

gprof - this is a good as there is:
GNU gprof
# 12  
Old 10-01-2009
Quote:
Originally Posted by Corona688
Optimization can cause errors in programs with subtle bugs. Places where accidental writing to stack or incidental return values hadn't mattered before, suddenly may. Proper programming practices will circumvent this, and the benefits of optimization can be very good.
Indeed. That's why I said I'll leave optimization as my last option.

First I'll make it fast and correct (hopefully bug free), then switch to optimization to make
it even faster (even if of a small percentage). At that stage, if a bugs come up, it is easier to find. At least, that's what I've learned.

---------- Post updated at 02:59 AM ---------- Previous update was at 02:51 AM ----------

Quote:
Originally Posted by jim mcnamara
Try a static link - that is one easy way to profile shared routines.
Hmm... interesting.

Quote:
Originally Posted by jim mcnamara
However try these as base assumptions:

most of the problems you have are yours, not library code
In fact, the library is mine! :-D I developed it.

Quote:
Originally Posted by jim mcnamara
algorithms and proper program design give the best return on optimization by far
code tricks result in very minor gains
Indeed! I'm not trying to achieve optimization with this kind of tricks.
As I said, I still haven't profiled my code and my post was only about how
to best resolve that particular problem (decoding a payload which byte order
is based on a header flag, which can only be known at runtime).

Quote:
Originally Posted by jim mcnamara
Do you know about aio (asynchronous I/O)? try reading up on that. It is often used in high demand high volume I/O design (disk). I think it is still not fully supported for sockets in Linux. I don't know for sure.
Yes. I've already read about aio and I did consider it. I might use it for dumping
the data, I receive from the net.

Quote:
Originally Posted by jim mcnamara
gprof - this is a good as there is:
GNU gprof

Thanks for the whole reply!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Function - Make your function return an exit status

Hi All, Good Day, seeking for your assistance on how to not perform my 2nd, 3rd,4th etc.. function if my 1st function is in else condition. #Body function1() { if then echo "exist" else echo "not exist" } #if not exist in function1 my all other function will not proceed.... (4 Replies)
Discussion started by: meister29
4 Replies

2. Programming

Segmentation fault when I pass a char pointer to a function in C.

I am passing a char* to the function "reverse" and when I execute it with gdb I get: Program received signal SIGSEGV, Segmentation fault. 0x000000000040083b in reverse (s=0x400b2b "hello") at pointersExample.c:72 72 *q = *p; Attached is the source code. I do not understand why... (9 Replies)
Discussion started by: jose_spain
9 Replies

3. Programming

Pure C function pointer on printing vowels twice

Have difficulty to understand this pure C code to only print vowels twice from input string. Questions are commented at the end of each place. #include <stdio.h> #include <stdlib.h> #include <assert.h> #include <limits.h> /* *Demonstrate the use of dispatch tables */ /*Print a char... (11 Replies)
Discussion started by: yifangt
11 Replies

4. Programming

Trivial doubt about C function pointer

Hi, In the below C code, #include <stdio.h> void print() { printf("Hello\n"); } int main() { void (*f)() = (void (*)()) print; f(); (*f)(); } I wonder, how the syntaxes "f()" and "(*f)()" are treated as same without any error? Is this an improvement or ANSI/ISO... (1 Reply)
Discussion started by: royalibrahim
1 Replies

5. Programming

structure pointer array as function parameters

if i create an array of pointers to a structure "struct node" as: struct node *r; and create "n" number of "linked lists" and assign it to the various struct pointers r using some function with a return type as structure pointer as: r=multiplty(.......) /*some parameters*/ is... (2 Replies)
Discussion started by: mscoder
2 Replies

6. Programming

Function Returning Pointer

Hi guys. how a functions such fdopen, ... can return pointer? are these functions use static memory(variables)? (6 Replies)
Discussion started by: majid.merkava
6 Replies

7. Programming

Inline function inside Classes

#include <iostream> using namespace std; class A { public: int Getvalue() { return i;} private: int i; }; int main() {} The above code compiles properly in g++ or in any other C++ compiler. BUT, the variable 'i' is used (in 'return i' statement) before it is... (1 Reply)
Discussion started by: deepthi.s
1 Replies

8. Programming

How to return void function pointer

Hello all im trying to build function that will return void function pointer what is mean is ( not working ) the main function void * myClass::getFunction(int type){ if(type==1) return &myClass::Test1; if(type==2) return &myClass::Test2; } void myClass::Test1(){... (1 Reply)
Discussion started by: umen
1 Replies

9. Programming

Problem with function which reutrns pointer to a value

i have a function: char *pcCityIdToCountryName(ADMIN_DB_DATA *pstHEader, unit uiCityID) this returns a pointer to CountryName if cityId is given. to retrieve countryname i give: char *CountryName; CountryName = pcCityIdToCountryName(..................); but when i compile it is giving :... (5 Replies)
Discussion started by: jazz
5 Replies

10. Programming

Will exe file size decrease while using inline function

using namespace std; void g(); class A { public : A() { g();g();g(); cout << "Constructor of A"<< endl ;} }; inline void g(){ cout << "vijay" <<endl; } int main() { A a; } when i use inline i get size 303488 Aug 31 12:05 a.out* when not using inline i get size 303572 Aug 31... (1 Reply)
Discussion started by: vijaysabari
1 Replies
Login or Register to Ask a Question