By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,148 Members | 1,373 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,148 IT Pros & Developers. It's quick & easy.

which is faster?

P: n/a
Is accessing function by it's name faster or accessing it by its
address faster?

Jun 29 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Sunil Varma said:
Is accessing function by it's name faster or accessing it by its
address faster?


The name of a function /is/ its address.

The Standard says (according to a late C89 draft):

"A function designator is an expression that has function type.
Except when it is the operand of the sizeof operator /25/ [1] or the unary
& operator, a function designator with type ``function returning type
'' is converted to an expression that has type ``pointer to function
returning type .''"
[1] Footnote 25 isn't really relevant for the purpose of this question - it
just reminds us that making a function designator the operand of the sizeof
operator violates a constraint.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jun 29 '06 #2

P: n/a
In article <11**********************@y41g2000cwy.googlegroups .com>,
Sunil Varma <su*******@gmail.com> wrote:
Is accessing function by it's name faster or accessing it by its
address faster?


There's no runtime name lookup necessary in C, if that's what you
mean. Or perhaps you mean is it faster to access a function through a
pointer variable? If so, I believe that on most architectures it is
faster to have a constant address. But this is not a C issue.

-- Richard
Jun 29 '06 #3

P: n/a
Sunil Varma wrote:
Is accessing function by it's name faster or accessing it by its
address faster?


Probably. Do you have a specific reason for caring?

--
Chris "micro-optimisations are NOT us" Dollin
A rock is not a fact. A rock is a rock.

Jun 29 '06 #4

P: n/a
Sunil Varma posted:
Is accessing function by it's name faster or accessing it by its
address faster?

Basically you're asking whether it's faster to access an object directly,
or to ask it via indirection.

#include <stdio.h>

void Print(void)
{
puts("Hello!\n");
}

int main(void)
{
void (*const pFunc)(void) = Print;

void (*const*const ppFunc)(void) = &pFunc;

void (*const*const*const pppFunc)(void) = &ppFunc;

void (*const*const*const*const ppppFunc)(void) = &pppFunc;

void (*const*const*const*const*const pppppFunc)(void) = &ppppFunc;

void (*const*const*const*const*const*const ppppppFunc)(void) =
&pppppFunc;

(****pppppFunc)();

}
--

Frederick Gotham
Jun 29 '06 #5

P: n/a
Frederick Gotham said:
Sunil Varma posted:
Is accessing function by it's name faster or accessing it by its
address faster?

Basically you're asking whether it's faster to access an object directly,
or to ask it via indirection.


No, he isn't. Functions aren't objects, and he was asking specifically about
functions.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jun 29 '06 #6

P: n/a
Richard Heathfield posted:
Frederick Gotham said:
Sunil Varma posted:
Is accessing function by it's name faster or accessing it by its
address faster?

Basically you're asking whether it's faster to access an object
directly, or to ask it via indirection.


No, he isn't. Functions aren't objects, and he was asking specifically
about functions.

Yes I realise. I worded my post badly.

Basically, would it be faster to locate *something* in memory by having its
address, or by having an address at which resides an address at which
resides an address at which resides an address at which resides an address
at which resides an address at which resides the thing you want in memory?

Obviously, we'd like as little redirection as possible (whether it be an
object we're looking for, or a function).

--

Frederick Gotham
Jun 29 '06 #7

P: n/a
Frederick Gotham wrote:

Sunil Varma posted:
Is accessing function by it's name faster or accessing it by its
address faster?


Basically you're asking whether
it's faster to access an object directly,
or to ask it via indirection.


Not really.
The only difference is that the name of a function
converts to a constant address in a function call,
whereas a pointer expression may be either a constant or a variable.
In case where there is a difference in speed,
I would expect a constant to be faster than a variable.

(putchar)('A');

should compile the same as

(&putchar)('A');

The only times that a function name doesn't
convert implicitly to a pointer to the function,
is as an operand to the address operator &,
and as an operand of the sizeof operator,
which is why (sizeof function_name) is undefined.

So, it's not a matter of differences in indirection.

--
pete
Jun 29 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.