Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Function pointer vs conditional branch

Reply
Thread Tools

Function pointer vs conditional branch

 
 
Edward Rutherford
Guest
Posts: n/a
 
      01-04-2012
Hello

As a general rule-of-thumb: Are function pointers more efficient than if
statements, if the same path will be taken throughout the course of the
program?

E.g. If I was going to repeatedly render an image in one of three ways,
and the method of rendering it gets selected at the beginning of
execution: Would it be more efficient to use a function pointer instead
of an if statement?

Thanks.
 
Reply With Quote
 
 
 
 
Kaz Kylheku
Guest
Posts: n/a
 
      01-04-2012
On 2012-01-04, Edward Rutherford <(E-Mail Removed)> wrote:
> Hello
>
> As a general rule-of-thumb: Are function pointers more efficient than if
> statements, if the same path will be taken throughout the course of the
> program?
>
> E.g. If I was going to repeatedly render an image in one of three ways,
> and the method of rendering it gets selected at the beginning of
> execution: Would it be more efficient to use a function pointer instead
> of an if statement?


An if statement selects a body of code which is still within the same lexical
scope, whereas a function call branches into a different scope. So the program
organization perspective is the most important. Obviously, if you have to
select from among multiple execution branches, which all require access to the
same set of local variables, you cannot use a function call (unless you go out
of your way to emulate lexical closures).

Are you asking which of these is better:

if (condition1) {
func1(...);
} else if (condition2) {
func2(...);
} else {
func3(...);
}

versus:

if (condition1) {
fptr = func1;
} else if (condition2) {
fptr = func2;
} else {
fptr = func3;
}

(*fptr)(...);

Stated in this way, the rewrite is unlikely to be fruitful in obtaining a
performance improvement, and may even be worse.

To obtain the benefit of using the function pointer, you have to turn your
attention into how that pointer is obtained.

Perhaps the conditions are numeric and can be put into a static dispatch
table, function pointers.

However, note that compilers do these kinds of optimizations. Code like:

switch (x) {
case 0:
func0(...);
break;
case 1:
func1(...);
break;
...
case N:
func1(...);
break;
default:
foo();
}

may well be compiled into an indirect jump through a hidden table indexed by N:

if (x >= 0 && x <= N)
hidden_table[x](...)
else
foo();

so unless you verify that the compiler isn't doing it, it wouldn't be worth it
to do it manually (other than for improving the code).

Such a table dispatch is almost certainly going to be faster than a naive
compile of the switch, because on modern CPU's it's faster to fetch some data
from a table and branch once than to go through two branches (as a rule of
thumb). Branches disrupt pipelines, requiring techniques like scheduling
instructions in branch delay slots, and branch prediction.

Another question is, how often does the three-way decision have to be
re-evaluated? Is it always different for each rendering job?

If the same choice is applied to multiple rendering jobs, it may help to
represent that by a context structure which has the function pointer in it,
already initialized to point to the correct function whenever the variables
are updated which influence the decision. There is no decision to make at
render time: just retrieve the pointer and call it:

render_context->render(render_context, ...);

How complicated are the conditions being evaluated to make the three-way
switch? Does it boil down to dispatch based on a numeric code, or something
else?

What is more frequently occuring: rendering jobs, or changes to the conditions
that influence the three-way choice?
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      01-04-2012
On 01/ 5/12 11:20 AM, Edward Rutherford wrote:
> Hello
>
> As a general rule-of-thumb: Are function pointers more efficient than if
> statements, if the same path will be taken throughout the course of the
> program?
>
> E.g. If I was going to repeatedly render an image in one of three ways,
> and the method of rendering it gets selected at the beginning of
> execution: Would it be more efficient to use a function pointer instead
> of an if statement?


There isn't a general rule-of-thumb answer, there ate too many variables
to consider.

--
Ian Collins
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      01-05-2012
On 1/4/2012 5:20 PM, Edward Rutherford wrote:
> Hello
>
> As a general rule-of-thumb: Are function pointers more efficient than if
> statements, if the same path will be taken throughout the course of the
> program?


"I have it on the best authority." -- Arthur

> E.g. If I was going to repeatedly render an image in one of three ways,
> and the method of rendering it gets selected at the beginning of
> execution: Would it be more efficient to use a function pointer instead
> of an if statement?


Mu.

Here's a counter-question: Does it matter? Estimate, pray,
the amount of time spent dispatching to one rendering function or
another versus the amount of time spent in that function actually
doing the rendering. Let's see: A smallish 640x480 image has 50K
pixels, depending on image characteristics that's anywhere from
50K to 200K values to compute, 50K pixel-to-pixel navigations to
figure out ("Have I hit the end of a row?"), ... And you're
worried about the once-per-rendering cost of deciding which
function to call?

I've mentioned an old friend's metaphor more than once, but
I think it applies rather strongly here: You're cleaning the bottle
caps and cigarette butts off the beach, to make the sand nice and
neat around the whale carcases.

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)d
 
Reply With Quote
 
Kaz Kylheku
Guest
Posts: n/a
 
      01-05-2012
On 2012-01-05, Eric Sosman <(E-Mail Removed)> wrote:
> Here's a counter-question: Does it matter? Estimate, pray,
> the amount of time spent dispatching to one rendering function or
> another versus the amount of time spent in that function actually
> doing the rendering. Let's see: A smallish 640x480 image has 50K
> pixels, depending on image characteristics that's anywhere from
> 50K to 200K values to compute, 50K pixel-to-pixel navigations to
> figure out ("Have I hit the end of a row?"), ... And you're
> worried about the once-per-rendering cost of deciding which
> function to call?


My reaction (hitherto unwritten) was that he must be just handing the images
off to some very fast hardware to care about the dispatch speed.

> I've mentioned an old friend's metaphor more than once, but
> I think it applies rather strongly here: You're cleaning the bottle
> caps and cigarette butts off the beach, to make the sand nice and
> neat around the whale carcases.


That little beauty doesn't apply that well; if you keep using it, you will soon
wear a hole right through!

Here, we may have a case of borrowing a motorcycle for the last 50 meters
of a marathon.

(Well, that might be attractive, depending on what shape you're in, but
it won't make a big difference to your time.)
 
Reply With Quote
 
BartC
Guest
Posts: n/a
 
      01-05-2012


"Edward Rutherford" <(E-Mail Removed)> wrote in
message news:je2jb2$886$(E-Mail Removed)...
> Hello
>
> As a general rule-of-thumb: Are function pointers more efficient than if
> statements, if the same path will be taken throughout the course of the
> program?
>
> E.g. If I was going to repeatedly render an image in one of three ways,
> and the method of rendering it gets selected at the beginning of
> execution: Would it be more efficient to use a function pointer instead
> of an if statement?


How many times will that if statement or function dispatch get executed? For
example, once per image, once per row, or once per pixel?

--
Bartc

 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      01-05-2012
On 1/4/2012 8:25 PM, Kaz Kylheku wrote:
> On 2012-01-05, Eric Sosman<(E-Mail Removed)> wrote:
>> [...]
>> I've mentioned an old friend's metaphor more than once, but
>> I think it applies rather strongly here: You're cleaning the bottle
>> caps and cigarette butts off the beach, to make the sand nice and
>> neat around the whale carcases.

>
> That little beauty doesn't apply that well; if you keep using it, you will soon
> wear a hole right through!


I've also used one about waxing your car to improve fuel economy:
Does it help by making the car slipperier, or hurt by increasing the
weight, and how do these effects compare to those of the underinflated
tires, the empty boat trailer dragging along behind, and the driver's
lamentable fondness for jackrabbit starts?

> Here, we may have a case of borrowing a motorcycle for the last 50 meters
> of a marathon.


Nice! I'll add it to the repertoire, if you don't mind/

--
Eric Sosman
(E-Mail Removed)d
 
Reply With Quote
 
Gene
Guest
Posts: n/a
 
      01-05-2012
On Jan 4, 5:20*pm, Edward Rutherford
<(E-Mail Removed)> wrote:
> Hello
>
> As a general rule-of-thumb: Are function pointers more efficient than if
> statements, if the same path will be taken throughout the course of the
> program?
>
> E.g. If I was going to repeatedly render an image in one of three ways,
> and the method of rendering it gets selected at the beginning of
> execution: Would it be more efficient to use a function pointer instead
> of an if statement?


Rendering an image will involve billions of instructions. Branching
to the start of the rendering algorithm is one or two instructions.
The choice doesn't matter.

Now if the branch is in the inner loop being executed once per pixel,
it _might_ make a small, very processor-dependent difference. Modern
processors often have some kind of branch prediction or split
execution path to optimize pipeline behavior for conditional branch
instructions. These mechanisms don't normally work for function
pointers. So for a 2- or 3-way branch, you'd probably want to first
try the natural if...then...else. As always, profile and tweak only
if you're sure the simplest coding isn't fast enough.

 
Reply With Quote
 
Kaz Kylheku
Guest
Posts: n/a
 
      01-05-2012
On 2012-01-05, Eric Sosman <(E-Mail Removed)> wrote:
> On 1/4/2012 8:25 PM, Kaz Kylheku wrote:
>> On 2012-01-05, Eric Sosman<(E-Mail Removed)> wrote:
>>> [...]
>>> I've mentioned an old friend's metaphor more than once, but
>>> I think it applies rather strongly here: You're cleaning the bottle
>>> caps and cigarette butts off the beach, to make the sand nice and
>>> neat around the whale carcases.

>>
>> That little beauty doesn't apply that well; if you keep using it, you will soon
>> wear a hole right through!

>
> I've also used one about waxing your car to improve fuel economy:
> Does it help by making the car slipperier, or hurt by increasing the
> weight, and how do these effects compare to those of the underinflated
> tires, the empty boat trailer dragging along behind, and the driver's
> lamentable fondness for jackrabbit starts?


Washing and waxing the car smoothes out the engine idle, lowers the exhaust
rattle by 15 dB and causes the clutch to engage like it's leather-padded.
 
Reply With Quote
 
88888 Dihedral
Guest
Posts: n/a
 
      01-05-2012
Edward Rutherford於 2012年1月5日星期四UTC+8上午6時20分18秒 道:
> Hello
>
> As a general rule-of-thumb: Are function pointers more efficient than if
> statements, if the same path will be taken throughout the course of the
> program?
>
> E.g. If I was going to repeatedly render an image in one of three ways,
> and the method of rendering it gets selected at the beginning of
> execution: Would it be more efficient to use a function pointer instead
> of an if statement?
>
> Thanks.


I'll give an example to illustrate my understanding of the questions in your
post in pseudo code.

//Filter all gray levels equual or above 170 to be 255 and those bellows to 0.

For each pixel in the image:
if gray>=170 store 255 ;
else store 0;

For each pixel in the image:
store map[gray];

Which will work better?





 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
diffrence between "function pointer" and "pointer to a function" murgan C Programming 6 12-21-2005 06:01 AM
Passing pointer to template function as argument to pointer to template function Vijai Kalyan C++ 4 11-08-2005 07:53 PM
? ELSE Conditional Comment / Using Conditional Comments Inside Other Tags To Comment Out Attributes Alec S. HTML 10 04-16-2005 02:21 AM
pointer to member function and pointer to constant member function Fraser Ross C++ 4 08-14-2004 06:00 PM
function pointer and member function pointer question glen stark C++ 2 10-10-2003 01:41 PM



Advertisments