int func(int) versus int func(int *) efficacy. 
Author Message
 int func(int) versus int func(int *) efficacy.

Hello,

I am not a professional programmer.

I have been told that the function:  ' int func(int) '  is faster then
the function ' int func(int *) ' .

Obviously, both functions do the same.

If anybody knows why, I will appreciate the answer.

Thank You Very Much,




Mon, 19 Mar 2001 03:00:00 GMT  
 int func(int) versus int func(int *) efficacy.

   I have been told that the function:  ' int func(int) '  is faster then
   the function ' int func(int *) ' .

   Obviously, both functions do the same.

No, they do not do the same thing.  The former takes an int argument,
the latter takes a pointer to int argument.
--
(supporter of the campaign for grumpiness where grumpiness is due in c.l.c)

Please: do not email me copies of your posts to comp.lang.c
        do not ask me C questions via email; post them instead



Mon, 19 Mar 2001 03:00:00 GMT  
 int func(int) versus int func(int *) efficacy.
See below for comments -RH- ...

Ori Gill wrote ...
...

Quote:
>I have been told that the function:  ' int func(int) '  is faster then
>the function ' int func(int *) ' .

-RH- Depends on the function. I think what the individual means is that
using 'n' versus '*pn' is faster. I suppose it would be, but in many cases
you need the address of the variable and not just what it is.
Quote:

>Obviously, both functions do the same.

-RH- Depends on the function. The easiest way to see the difference is to
write your own and try them out.
...


Mon, 19 Mar 2001 03:00:00 GMT  
 int func(int) versus int func(int *) efficacy.

Ori:

Quote:
>Hello,

>I am not a professional programmer.

>I have been told that the function:  ' int func(int) '  is faster then
>the function ' int func(int *) ' .

Either it is faster, slower or the same.  That's called trichotomy.

Quote:
>Obviously, both functions do the same.

Sort of.  Both can accomplish the same purpose, perhaps.

Quote:
>If anybody knows why, I will appreciate the answer.

Why?  You mean why pass an address rather than the value?  The answer is not
for speed, either way.  You pass the address if you are going to change the
contents of the variable in the called function, and you want it to change in
the routine that calls the function also.  If you just want the value passed,
then  you don't use an address.  Someone who passes the address because they
think it is faster is incompetent and needs further training.

Quote:
>Thank You Very Much,

You're double-extra peachy welcome.
--
C-FAQ ftp sites: ftp://ftp.eskimo.com ftp://rtfm.mit.edu
Hypertext C-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-FAQ Book: ISBN 0-201-84519-9.
Want Software?  Algorithms?  Pubs? http://www.infoseek.com


Tue, 20 Mar 2001 03:00:00 GMT  
 int func(int) versus int func(int *) efficacy.

Quote:

> Why?  You mean why pass an address rather than the value?  The answer is not
> for speed, either way.  You pass the address if you are going to change the
> contents of the variable in the called function, and you want it to change in
> the routine that calls the function also.  If you just want the value passed,
> then  you don't use an address.  Someone who passes the address because they
> think it is faster is incompetent and needs further training.

It should be clarified that this statement only applies to types such as
int. For large user defined types, it is usually more efficient to use
pointers rather than copying large amounts of data to/from functions.

--
Joe



Tue, 20 Mar 2001 03:00:00 GMT  
 int func(int) versus int func(int *) efficacy.
Hello,

I know that...

I meant to say that both functions are used for the same _purpose_ .

Quote:


>    I have been told that the function:  ' int func(int) '  is faster then
>    the function ' int func(int *) ' .

>    Obviously, both functions do the same.

> No, they do not do the same thing.  The former takes an int argument,
> the latter takes a pointer to int argument.
> --
> (supporter of the campaign for grumpiness where grumpiness is due in c.l.c)

> Please: do not email me copies of your posts to comp.lang.c
>         do not ask me C questions via email; post them instead



Tue, 20 Mar 2001 03:00:00 GMT  
 int func(int) versus int func(int *) efficacy.
Hello again,

" -RH- Depends on the function. I think what the individual means is that
 using 'n' versus '*pn' is faster. I suppose it would be, but in many cases
 you need the address of the variable and not just what it is. "

This is exactly what I meant, and by the time I am sending this message no
reply answered the question.

I know when I should pass a pointer to a variable or when to pass the
variable's value, and I am sorry if I failed to explain the question.

I asked the question just because I am curious:

Why transferring an ' int ' ("int variable") is faster then transferring a
pointer to ' int '(" int *p_variable") ?
In both cased the function returns an 'int' :

Calling to int func(int) is faster then to  int func(int *) .

If any body knows the technical reason to this specific question, I will
be happy to hear about it.

Thanks again,

Ori Gill

Quote:

> See below for comments -RH- ...

> Ori Gill wrote ...
> ...
> >I have been told that the function:  ' int func(int) '  is faster then
> >the function ' int func(int *) ' .
> -RH- Depends on the function. I think what the individual means is that
> using 'n' versus '*pn' is faster. I suppose it would be, but in many cases
> you need the address of the variable and not just what it is.

> >Obviously, both functions do the same.
> -RH- Depends on the function. The easiest way to see the difference is to
> write your own and try them out.
> ...



Tue, 20 Mar 2001 03:00:00 GMT  
 int func(int) versus int func(int *) efficacy.

Quote:
>Why transferring an ' int ' ("int variable") is faster then transferring a
>pointer to ' int '(" int *p_variable") ?
>In both cased the function returns an 'int' :

>Calling to int func(int) is faster then to  int func(int *) .

>If any body knows the technical reason to this specific question, I will
>be happy to hear about it.

I don't think that the calling sequence is faster in one case than the
other.  But the function itself can be better optimized if it takes an
int argument instead of an int pointer argument.  Pointers are great for
flexibility, but they restrict the number of safe optimizations that the
compiler can perform.  Moreover, accessing a variable thru a pointer is
usually slower, because it takes an additional memory accesses.

Bottom line: use pointers only if and when you have to.

Dan
--
Dan Pop
CERN, IT Division

Mail:  CERN - EP, Bat. 31 1-014, CH-1211 Geneve 23, Switzerland



Tue, 20 Mar 2001 03:00:00 GMT  
 int func(int) versus int func(int *) efficacy.

Quote:

> I am not a professional programmer.

> I have been told that the function:  ' int func(int) '  is faster then
> the function ' int func(int *) ' .

Hi Ori Gill,

Please drop this idea. If there is a difference at all, it is very
*very* small. And it will totally compiler dependant. I.e. on compiler
A the first might be 5 microseconds faster and on compiler B it's 7
microseconds slower.

Quote:
> Obviously, both functions do the same.

No, they do not. The first function get's an integer and the second
function receives an pointer to an integer.

Stephan
(initiator of the campaign against grumpiness in c.l.c)



Tue, 20 Mar 2001 03:00:00 GMT  
 int func(int) versus int func(int *) efficacy.

Quote:

>I asked the question just because I am curious:

>Why transferring an ' int ' ("int variable") is faster then transferring a
>pointer to ' int '(" int *p_variable") ?
>In both cased the function returns an 'int' :

>Calling to int func(int) is faster then to  int func(int *) .

>If any body knows the technical reason to this specific question, I will
>be happy to hear about it.

It may be on a particular platform the code that handles the arguments
is less efficient with one or the other. One thing to note is that to
use the value of the int * argument, code will probably be generated
to fetch the value at the address that was passed. That's an extra
step that doesn't have to be performed if the value is passed directly.

--
Craig

Manchester, NH
The most savage controversies are those about matters as to
which there is no good evidence either way. -- Bertrand Russell



Tue, 20 Mar 2001 03:00:00 GMT  
 int func(int) versus int func(int *) efficacy.

Quote:


>> Why?  You mean why pass an address rather than the value?  The answer is
not
>> for speed, either way.  You pass the address if you are going to change
the
>> contents of the variable in the called function, and you want it to
change in
>> the routine that calls the function also.  If you just want the value
passed,
>> then  you don't use an address.  Someone who passes the address because
they
>> think it is faster is incompetent and needs further training.

>It should be clarified that this statement only applies to types such as
>int. For large user defined types, it is usually more efficient to use
>pointers rather than copying large amounts of data to/from functions.

I am of the opinion that unless the data object is simply enormous or there
is a proven bottleneck, this aspect should be less of a concern today than
it was 20 years ago.  Passing by value is a very good protection from
corrupting your data.  Passing by address may be faster for large objects,
but it is still dangerous.  I design for "by value" when I want to pass the
value.  Then, if a profile session shows that a nested function call is a
bottleneck, I will change -- reluctantly -- to by address.
--
Hypertext C-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-FAQ ftp: ftp://rtfm.mit.edu, C-FAQ Book: ISBN 0-201-84519-9
Try "C Programming: A Modern Approach" ISBN 0-393-96945-2
Want Software?  Algorithms?  Pubs? http://www.infoseek.com


Tue, 20 Mar 2001 03:00:00 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. How to do int func(int size,int matrix[][size])

2. (int/int) != int

3. Calling int f(int (*f)(int)) like function in DLL from VB

4. INT versus int, which to use?

5. URGENT: int func( int t, int k, char s[t][k] ) right ?

6. help please: void/ int func. decltns

7. int func(char **)

8. how to call C++ func(int &var) from C.

9. int (*func)( void *, void*)

10. Help returning multiple int's from func...

11. cast a pointer to func returning int

12. extern int foo(int); vs int foo(int);

 

 
Powered by phpBB® Forum Software