Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Is there a difference between

C#
int test = 1;
while(...)
{
    test = 1;
}

and
C#
while(...)
{
    int test = 1;
}

Thanks for your response!
Posted
Updated 9-Nov-12 4:03am
v2
Comments
Albert Holguin 20-Nov-12 14:32pm    
Keep in mind that declaring a variable within a loop is illegal in some older C standards.

There is! In the first form the variable is visible outside of the while loop, whilst in the second form the variable is only visible inside the while loop. It is called variable scope[^].
If you need the variable after the loop has exited, then you should use the first form. The second form should be used if the variable will not be needed after the while loop. Using variables declared outside of a loop without actually needing that value outside will add confusion. If the value of that variable is not needed outside then declare (and define) a variable inside the loop.

Regards,

— Manfred
 
Share this answer
 
v3
Comments
Andi Vogel 9-Nov-12 11:40am    
Think there additionally is a difference in performance. If you do not always while running through the 'while' loop initialize and destroy the variable, but only fill it with new values this should be faster or?
Manfred Rudolf Bihy 9-Nov-12 11:57am    
Optimizing compilers already do stuff like that, i.e. the variable is already local to the containing method, it is just the compiler that watches out that it will not be accessed from places where it is semantically out of scope. This means that using variables definitions within loops will not incur a penalty when you have a good optimizing compiler.
You'd have to decompile / disassemble your compiler output to see what how your compiler performs.

Cheers!
Philippe Mori 9-Nov-12 18:44pm    
For simple types, there should be no difference in performance. It will matters for complex type or if an expression is recomputed inside the loop when it need to be done only once.
CPallini 12-Nov-12 8:16am    
5.
With the following code snippet the difference of performance between the two ways of variable declaration can be checked:

C++
long double sysTime1;
long double time1;
long double sysTime2;
long double time2;

sysTime1 = GetTickCount();

// First way
CString test;
int i;
for (i = 0; i < 1000000; i++)
    test.Format("Test%d", i+1);

time1 = GetTickCount() - sysTime1;

sysTime2 = GetTickCount();

// Second way
for (int i = 0; i < 1000000; i++)
{
    CString test;
    test.Format("Test%d", i+1);
}

time2 = GetTickCount() - sysTime2;

CString difference;
difference.Format("Difference: %lf ms", time2-time1);

MessageBox(NULL, difference, "Performance Test", MB_OK);

The differences on my windows system are:

Compiled with /Od : ca. 700 ms
Compiled with /O2 : ca. 600 ms
Compiled with /Ox : ca. 550 ms

Conclusion:

In time critical functions the first way of declaration should be used.

Best regards

sja
 
Share this answer
 
v2
"In-loop" declaration of variables and objects in the way

C++
for (int i...)
{
	CString test = ...
	...
}


fulfills the paradigms of object-oriented programming like encapsulation, data hiding and visibility.

The problem of wasting performance is on the compiler developing side and a solution can start with the following pseudocode:

C++
if in "while", "for", "do-while" loops do
	if "first cycle" do
		initialize and create "in-loop" objects
		assignments and processing...
	end
	else if "last cycle" do
		assignments and processing...
		destroy "in-loop" objects, free memory etc.
	end
	else do
		assignments and processing...
	end
end


Best regards
 
Share this answer
 
v2
Yes There is difference
In first the varibale is accessible inside the loop and outside the loop.
In second the variable is only accessible inside the loop.

In First The Variable is only one time Declared.
But in loop the garbage collector destroy it and it will again and again
declared.
 
Share this answer
 
Comments
sja63 12-Nov-12 11:00am    
The question is:

How is it possible to use the compact, modern and elegant way of "in-loop" declaration like

for (int i = 0; ...)
{
CString test1 = ...
CString test2 = ...
...
}

without loss of performance.

Any idea?
Legor 14-Nov-12 7:11am    
With a good compiler you wouldn't have to care about performance loss. It will not free and reallocate the memory if it is not needed.
TalalAslam 13-Nov-12 5:22am    
The Compact Way is not to declare variable in the loop
you can do this
CString test1=....
CString test2=....
for (int i =0;....)
{
test1 processing..
test2 processing...
}
in this way the test1 and test2 is declared only once and can also accessible out side the loop
but in your written way the test1 and test 2 will again and declared and garbage collector will destroy it each time so that performance will be decrease, also test1 and test2 will only accessible inside loop.
have a good time dear firenad

at first code, the "test" variable is global for the "while" and after the "while" it will be and can be used and memory will keep it's value.
but in the second code, the "test" is local in the "while" and after the "while" it will not be used to code.
 
Share this answer
 
C#
int test = 1;
while(...)
{
    test = 1;
}


The above coding is good practice. Beacuse declaration of variable is done before the for loop, it make only one int variable and use the memory of one int(4 bytes).

C#
while(...)
{
    int test = 1;
}



In this case suppose n iterations done in while loop. So create integer varibales n times. So the previous value is not considering and stack range increases.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900