For Programmers: Free Programming Magazines  


Home > Archive > C > June 2006 > which is faster?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author which is faster?
Sunil Varma

2006-06-29, 7:56 am

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

Richard Heathfield

2006-06-29, 7:56 am

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)
Richard Tobin

2006-06-29, 7:56 am

In article <1151580397.993588.283560@y41g2000cwy.googlegroups.com>,
Sunil Varma <sunil.s51@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
Chris Dollin

2006-06-29, 7:56 am

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.

Frederick Gotham

2006-06-29, 6:56 pm

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
Richard Heathfield

2006-06-29, 6:56 pm

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.

--
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)
Frederick Gotham

2006-06-29, 6:56 pm

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

--

Frederick Gotham
pete

2006-06-29, 6:56 pm

Frederick Gotham wrote:
>
> 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.

--
pete
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com