Cost of Function Pointers (using them)?

Started by
25 comments, last by crakinshot 20 years, 5 months ago
out of intrest, why do you want to know the number of ticks?
I''m not convinced this would be a static number anyways depending on how the rest of the pipeline is working and how instructions are being decoded (while not being a compiler theory expert myself its possible that the cpu could start looking up the function pointer to be called before it needs to be called and interlacing other operations around it...)
Advertisement
quote:Original post by crakinshot
hehe... all I asked for in the beginning was the number of ticks... A simple number would of sufficed...
Dude, you''re missing the point. You don''t need this information. That''s why no one here knows the answer. It does not matter.

And besides, it''s probably compiler and platform dependant anyway.
someone must know how many ticks it takes... if thats all that he wants someone give it to him..lol
42 ?

As other people pointed out, it depends on the CPU, the state of the pipeline and how the code has been laid out by the compiler.

There is no answer. He could, however, write his own test case and time it himself. This smacks of laziness.

[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

[edited by - Fruny on November 8, 2003 2:33:37 PM]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Just do some simple testing... it really shouldn''t be all that complex, and if it''s your only option, you don''t have much of a choice anyways . It''s the most efficient option you''ve got, so not much reason to look for specific speeds, unless you think there are faster/more efficient ways.
You are talking about philosophy!!!
Calling a standard class member function is equal to call every other function (there is a pointer that points to the code...).
With virtual functions the mechanism is the same; the difference is that the compiler insert the code to get a pointer to a function pointer(in the vfptr_table): you have the extra work to get ONE pointer!
If your virtual function does something your overhead will be insignificant...(10%? 5%? 1%?).





I imagine a function pointer would be less expensive. With virtual functions each object has a pointer to a table of function pointers, which is used to look up which function to execute, so you have some extra addition and dereferencing.

I agree though, the time needed to do the look up will probably be virtually nothing compared to the time it actually takes the function you're calling to execute, making worrying about it pretty pointless.

[edited by - smart_idiot on November 8, 2003 8:37:14 PM]
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.

This topic is closed to new replies.

Advertisement