Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficiency of C Variable Declaration [duplicate]

How long does it take to declare a variable in C, for example int x or unsigned long long var? I am wondering if it would make my code any faster in something like this.

for (conditions) {
    int var = 0;
    // code
}

Would it be faster to do this, or is it easier not to?

int var;
for (conditions) {
    var = 0;
    // code
}

Thanks for the help.

like image 831
Michael Dickens Avatar asked Dec 24 '09 02:12

Michael Dickens


People also ask

Can you declare a variable twice in C?

int a; means both declaration and definition, but we know that defining a variable more than once is not allowed.

Is it good to declare variable in loop?

This is excellent practice. By creating variables inside loops, you ensure their scope is restricted to inside the loop. It cannot be referenced nor called outside of the loop.

Can we declare same variable in two times globally?

A variable or function can be declared any number of times, but it can be defined only once. (Remember the basic principle that you can't have two locations of the same variable or function).

Is it faster to declare variable outside loop?

There's no difference in performance between defining a simple variable (like an int) in a loop vs. outside the loop.


2 Answers

One piece of advice: stop worrying about which language constructs are microscopically faster or slower than which others, and instead focus on which ones let you express yourself best.

Also, to find out where your code is spending time, use a profiler.

And as others have pointed out, declarations are purely compile-time things, they don't affect execution time.

like image 65
Ned Batchelder Avatar answered Sep 23 '22 15:09

Ned Batchelder


It doesn't make any difference. In a traditional implementation the declaration itself (excluding initialization) generates no machine instructions. Function prologue code typically allocates space in the stack for all local variables at once, regardless of where they are declared.

However, where you declare your local variables can affect the performance of your code indirectly, in theory at least. When you declare the variables as locally as possible (your first variant), in general case it results in smaller size of the stack frame reserved by the function for its local variables (since the same location in the stack can be shared by different local variables at different times). Having smaller stack frame reduces the general stack memory consumption, i.e. as nested function calls are performed stack size doesn't grow as fast (especially noticeable with recursive functions). It generally improves performance since new stack page allocations happen less often, and stack memory locality becomes better.

The latter considerations are platform-dependent, of course. It might have very little or no effect on your platform and/or for your applications.

like image 44
AnT Avatar answered Sep 23 '22 15:09

AnT