I'm writing very processor-intensive cryptography code (C#), so I'm looking for any performance gains, no matter how small. I've heard opinions both ways on this subject.
Is there any performance benefit at all to
int smallPrime, spGen; for (int i = 0; i
over this?
for (int i = 0; i
Does the compiler do this already?
There is no performance benefit at all.
All local variables are allocated when the stack frame for the method is created, so it doesn't matter where in the method you declare them. It's only the scope of the variables that differ between the codes, and that is only information that the compiler uses at compile time.
Edit:
To verify that there is no difference, I compiled the two cases and examined the generated machine code, and it is identical for the two cases:
Declaring variables outside the loop:
for (int i = 0; i
Declaring variables inside the loop:
for (int i = 0; i
Not really, the compiler will do that optimization for you.