Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Kaz Kylheku <kaz@kylheku.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Function pointer vs conditional branch |
| Date | 2012-01-04 22:46 +0000 |
| Organization | A noiseless patient Spider |
| Message-ID | <20120104142441.219@kylheku.com> (permalink) |
| References | <je2jb2$886$1@speranza.aioe.org> |
On 2012-01-04, Edward Rutherford <edward.p.rutherford79@REMOVETHIS.gmail.com> 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?
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Function pointer vs conditional branch Edward Rutherford <edward.p.rutherford79@REMOVETHIS.gmail.com> - 2012-01-04 22:20 +0000
Re: Function pointer vs conditional branch Kaz Kylheku <kaz@kylheku.com> - 2012-01-04 22:46 +0000
Re: Function pointer vs conditional branch Ian Collins <ian-news@hotmail.com> - 2012-01-05 11:47 +1300
Re: Function pointer vs conditional branch Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-01-04 20:15 -0500
Re: Function pointer vs conditional branch Kaz Kylheku <kaz@kylheku.com> - 2012-01-05 01:25 +0000
Re: Function pointer vs conditional branch Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-01-04 20:36 -0500
Re: Function pointer vs conditional branch Kaz Kylheku <kaz@kylheku.com> - 2012-01-05 06:02 +0000
Re: Function pointer vs conditional branch "BartC" <bc@freeuk.com> - 2012-01-05 01:25 +0000
Re: Function pointer vs conditional branch Gene <gene.ressler@gmail.com> - 2012-01-04 17:57 -0800
Re: Function pointer vs conditional branch gordonb.vj0cm@burditt.org (Gordon Burditt) - 2012-01-04 23:54 -0600
Re: Function pointer vs conditional branch 88888 Dihedral <dihedral88888@googlemail.com> - 2012-01-04 23:25 -0800
Re: Function pointer vs conditional branch 88888 Dihedral <dihedral88888@googlemail.com> - 2012-01-04 23:38 -0800
Re: Function pointer vs conditional branch Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2012-01-05 13:47 -0700
Re: Function pointer vs conditional branch Nobody <nobody@nowhere.com> - 2012-01-05 21:29 +0000
Re: Function pointer vs conditional branch Dr Nick <3-nospam@temporary-address.org.uk> - 2012-01-06 06:44 +0000
Re: Function pointer vs conditional branch BGB <cr88192@hotmail.com> - 2012-01-06 11:41 -0600
Re: Function pointer vs conditional branch Kaz Kylheku <kaz@kylheku.com> - 2012-01-06 18:33 +0000
Re: Function pointer vs conditional branch Willem <willem@toad.stack.nl> - 2012-01-06 21:41 +0000
Re: Function pointer vs conditional branch Stephen Sprunk <stephen@sprunk.org> - 2012-01-06 16:08 -0600
Re: Function pointer vs conditional branch Kaz Kylheku <kaz@kylheku.com> - 2012-01-06 22:35 +0000
Re: Function pointer vs conditional branch BGB <cr88192@hotmail.com> - 2012-01-06 17:00 -0600
Re: Function pointer vs conditional branch Kaz Kylheku <kaz@kylheku.com> - 2012-01-06 23:44 +0000
Re: Function pointer vs conditional branch Stephen Sprunk <stephen@sprunk.org> - 2012-01-06 17:08 -0600
Re: Function pointer vs conditional branch Kaz Kylheku <kaz@kylheku.com> - 2012-01-06 23:49 +0000
Re: Function pointer vs conditional branch jacob navia <jacob@spamsink.net> - 2012-01-07 00:55 +0100
Re: Function pointer vs conditional branch Kaz Kylheku <kaz@kylheku.com> - 2012-01-07 00:07 +0000
Re: Function pointer vs conditional branch "christian.bau" <christian.bau@cbau.wanadoo.co.uk> - 2012-01-07 06:14 -0800
Re: Function pointer vs conditional branch "BartC" <bc@freeuk.com> - 2012-01-07 14:44 +0000
Re: Function pointer vs conditional branch Robert Wessel <robertwessel2@yahoo.com> - 2012-01-07 14:37 -0600
Re: Function pointer vs conditional branch Kaz Kylheku <kaz@kylheku.com> - 2012-01-07 20:42 +0000
Re: Function pointer vs conditional branch Stephen Sprunk <stephen@sprunk.org> - 2012-01-07 16:47 -0600
Re: Function pointer vs conditional branch Robert Wessel <robertwessel2@yahoo.com> - 2012-01-08 01:49 -0600
Re: Function pointer vs conditional branch Kaz Kylheku <kaz@kylheku.com> - 2012-01-08 08:35 +0000
Re: Function pointer vs conditional branch Stephen Sprunk <stephen@sprunk.org> - 2012-01-07 17:02 -0600
Re: Function pointer vs conditional branch "BartC" <bc@freeuk.com> - 2012-01-06 21:29 +0000
Re: Function pointer vs conditional branch BGB <cr88192@hotmail.com> - 2012-01-06 17:15 -0600
Re: Function pointer vs conditional branch Michael Angelo Ravera <maravera@prodigy.net> - 2012-01-06 23:00 -0800
Re: Function pointer vs conditional branch jgk@panix.com (Joe keane) - 2012-01-07 21:04 +0000
csiph-web