Reputation: 309
If using a void function in C versus a function that returns an arbitrary type (say int), will int
function take more CPU cycles than a void
function?
Example:
int a, b = 1, c = 2;
void f()
{
a = b + c;
}
int g()
{
a = b + c;
return a;
}
My common sense tells me that a return is an action, so it should take some of the CPU time, but I don't have proper deep fundamental knowledge that is needed here, nor do I know assembler to answer this question confidently by myself. Googling around was not successful either.
Edit: My interest is purely academic and I don't expect to gain any noticeable (or even close to that) amount of performance by using void versus int functions.
Upvotes: 1
Views: 352
Reputation: 11
we cannot say that whether there will be extra cycles usage because it purely depends o your processor or cpu,and moreover if you need to return the value then return statement just accesses the memory location of that element only hence there may be only a small change in complexity which is negligible.
Upvotes: 0
Reputation:
On an x86_64 system, both functions can be compiled to the same code. I've "glossed" the disassembly with roughly equivalent C code:
f_or_g:
pushq %rbp ; // Standard stack frame setup
movq %rsp, %rbp ; // same
movl OFFSET1(%rip), %eax ; eax = c;
addl OFFSET2(%rip), %eax ; eax += b;
movq OFFSET3(%rip), %rcx ; rcx = &a;
movl %eax, (%rcx) ; *rcx = eax;
popq %rbp ; // Standard stack frame teardown
retq ; return
Since x86_64 uses eax
as the return register for 32-bit values, the result of the addition is in the "right place" to return it already -- no extra code is needed.
In more complex functions, there may be some minor overhead required to ensure that the return value ends up in the right register. Generally speaking, though, that overhead should be pretty minimal.
The same principle applies to most other architectures -- this isn't specific to x86_64; I'm just using it because that's the first compiler that came to hand.
Upvotes: 3
Reputation: 6395
I don't think the question makes much sense.
If you need the returned value, you don't have the option to use void
to speed it up - even if it would be faster.
If you don't need the result, it is useless to return it, so just don't do it.
Either way, the choice is defined by the needs of the caller.
Typically, modern compilers don't return the value, but construct it in place. For example if you write int sum = f(a,b);
the compiler will never make the temporary in your function, and instead use the memory of sum
to store the result. That means there is no difference in execution time.
Upvotes: 3
Reputation: 28050
That entirely depends on the CPU instruction set and calling conventions.
If, for example, the return value is always returned in a specific register, and the compiler can arrange the result of the calculation b+c to be in that specific register before inserting the return instruction, the code generated for those two functions may be identical.
However, this is not the kind of thing you want to think about optimizing in your program unless you exhausted all other options for performance improvements. And you certainly did not.
Upvotes: 5