Вот небольшой тест, который я сделал для оценки сценариев;
Код:
using System;
using System.Diagnostics;
namespace TestVariableScopePerformance
{
class Program
{
static void Main(string[] args)
{
TestClass tc = new TestClass();
Stopwatch sw = new Stopwatch();
sw.Start();
tc.MethodGlobal();
sw.Stop();
Console.WriteLine("Elapsed for MethodGlobal = {0} Minutes {1} Seconds {2} MilliSeconds", sw.Elapsed.Minutes, sw.Elapsed.Seconds, sw.Elapsed.Milliseconds);
sw.Reset();
sw.Start();
tc.MethodLocal();
sw.Stop();
Console.WriteLine("Elapsed for MethodLocal = {0} Minutes {1} Seconds {2} MilliSeconds", sw.Elapsed.Minutes, sw.Elapsed.Seconds, sw.Elapsed.Milliseconds);
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
}
}
class TestClass
{
const int Const1 = 100;
internal void MethodGlobal()
{
double temp = 0d;
for (int i = 0; i < int.MaxValue; i++)
{
temp = (i * Const1);
}
}
internal void MethodLocal()
{
const int Const2 = 100;
double temp = 0d;
for (int i = 0; i < int.MaxValue; i++)
{
temp = (i * Const2);
}
}
}
}
Результаты 3 итераций:
Elapsed for MethodGlobal = 0 Minutes 1 Seconds 285 MilliSeconds
Elapsed for MethodLocal = 0 Minutes 1 Seconds 1 MilliSeconds
Press any key to continue...
Elapsed for MethodGlobal = 0 Minutes 1 Seconds 39 MilliSeconds
Elapsed for MethodLocal = 0 Minutes 1 Seconds 274 MilliSeconds
Press any key to continue...
Elapsed for MethodGlobal = 0 Minutes 1 Seconds 305 MilliSeconds
Elapsed for MethodLocal = 0 Minutes 1 Seconds 31 MilliSeconds
Press any key to continue...
Я думаю,наблюдение завершает @ jnm2 ответ.
Запустите тот же код из вашей системы и сообщите нам результат.