recvezitor Цитата: Всякоразно попробовал
Заинтриговал. Действительно, тенденция наблюдается.
Но вот в таком варианте времена практически сравниваются:
Код: class Program
{
#region MinMax
public static double FindMax(double[] a)
{
int n = a.Length;
double max = a[0]/3.0;
for (int i = 1; i < n; i++)
{
if (a[i]/3 > max)
{
max = a[i]/3.0;
}
}
return max;
}
public static double FindMax(double[] a, out int index)
{
int n = a.Length;
double max = a[0]/3.0;
index = 0;
for (int i = 1; i < n; i++)
{
if (a[i]/3 > max)
{
max = a[i]/3.0;
index = i;
}
}
return max;
}
public static double FindMin(double[] a)
{
int n = a.Length;
double min = a[0];
for (int i = 1; i < n; i++)
{
if (a[i] < min) min = a[i];
}
return min;
}
public static double FindMin(double[] a, out int index)
{
int n = a.Length;
double min = a[0];
index = 0;
for (int i = 1; i < n; i++)
{
if (a[i] < min)
{
min = a[i];
index = i;
}
}
return min;
}
#endregion MinMax
static System.Timers.Timer timer1 = new System.Timers.Timer();
static double times;
static int testCount = 50;
static int elementsCount = 10000000;
static double[] valuesDouble = new double[elementsCount];
static int index = 0;
static double max = 0;
static double fTime = 0;
static double averageTime = 0;
static Random rand = new Random();
static double fwTime = 0;
static double fwoTime = 0;
static void Main(string[] args)
{
timer1.Elapsed += new System.Timers.ElapsedEventHandler(timer1_Elapsed);
timer1.Interval = 10.0;
times = 0.0;
WithoutIndexTests();
WithIndexTests();
WithIndexTests();
WithoutIndexTests();
WithIndexTests();
WithoutIndexTests();
WithoutIndexTests();
WithIndexTests();
WithIndexTests();
WithoutIndexTests();
WithIndexTests();
WithoutIndexTests();
Console.WriteLine("With index: {0}, without: {1}\n", fwTime, fwoTime);
}
static void fillArr(double[] a)
{
for (int i = 0; i < a.Length; i++)
{
a[i] = rand.NextDouble();
}
}
public static void WithIndexTests()
{
averageTime = 0; fTime = 0;
for (int i = 0; i < testCount; i++)
{
times = 0;
fillArr(valuesDouble);
timer1.Enabled = true;
max = FindMax(valuesDouble, out index);
timer1.Enabled = false;
Console.WriteLine("With index: time={0}, max={1}, index={2};", times, max, index);
fTime += times;
}
averageTime += fTime / testCount;
fwTime += averageTime;
Console.WriteLine("With index AVERAGE TIME: {0}\n", averageTime);
}
public static void WithoutIndexTests()
{
averageTime = 0; fTime = 0;
for (int i = 0; i < testCount; i++)
{
times = 0;
fillArr(valuesDouble);
timer1.Enabled = true;
max = FindMax(valuesDouble);
timer1.Enabled = false;
Console.WriteLine("Without index: time={0}, max={1};", times, max);
fTime += times;
}
averageTime = fTime / testCount;
fwoTime += averageTime;
Console.WriteLine("Without index AVERAGE TIME: {0}\n", averageTime);
}
public static void timer1_Elapsed(object sender, EventArgs e)
{
times += timer1.Interval;
}
}