which is faster?

S

Sunil Varma

Is accessing function by it's name faster or accessing it by its
address faster?
 
R

Richard Heathfield

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.
 
R

Richard Tobin

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
 
F

Frederick Gotham

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)();

}
 
R

Richard Heathfield

Frederick Gotham said:
Sunil Varma posted:



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.
 
F

Frederick Gotham

Richard Heathfield posted:
Frederick Gotham said:


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).
 
P

pete

Frederick said:
Sunil Varma posted:


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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
474,184
Messages
2,570,976
Members
47,536
Latest member
MistyLough

Latest Threads

Top