Connecting Tech Pros Worldwide Forums | Help | Site Map

Optimizer Query

C# Learner
Guest
 
Posts: n/a
#1: Nov 15 '05
With code like this:

public void SomeMethod()
{
for (int i = 0; i < SomeLargeNumber; ++i) {
int n = GetANumber();
DoSomethingWith(n);
}
}

Would the "int n" part be optimized out somewhere along the line?
i.e.: would something like the following happen instead:

public void SomeMethod()
{
int n;

for (int i = 0; i < SomeLargeNubmer; ++i) {
n = GetANumber();
DoSomethingWith(n);
}
}

Just curious...

Ignacio Machin \( .NET/ C# MVP \)
Guest
 
Posts: n/a
#2: Nov 15 '05

re: Optimizer Query



It will generate the same code, or at least one equivalent.

If what you are worried about is that each time the loop is executed a new
instance is created fear not, it does not happen, the variable n is created
in the stack and is created only once,


Hope this help,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

Learner" <csharp@learner.here> wrote in message
news:mtpv101nri61t41v6djej5s8e1u9ko7n8q@4ax.com...[color=blue]
> With code like this:
>
> public void SomeMethod()
> {
> for (int i = 0; i < SomeLargeNumber; ++i) {
> int n = GetANumber();
> DoSomethingWith(n);
> }
> }
>
> Would the "int n" part be optimized out somewhere along the line?
> i.e.: would something like the following happen instead:
>
> public void SomeMethod()
> {
> int n;
>
> for (int i = 0; i < SomeLargeNubmer; ++i) {
> n = GetANumber();
> DoSomethingWith(n);
> }
> }
>
> Just curious...[/color]


C# Learner
Guest
 
Posts: n/a
#3: Nov 15 '05

re: Optimizer Query


"Ignacio Machin \( .NET/ C# MVP \)" <ignacio.machin AT
dot.state.fl.us> wrote:
[color=blue]
>It will generate the same code, or at least one equivalent.
>
>If what you are worried about is that each time the loop is executed a new
>instance is created fear not, it does not happen, the variable n is created
>in the stack and is created only once,
>
>
>Hope this help,[/color]

Thankyou.
Closed Thread


Similar C# / C Sharp bytes