数据量不同显示的耗时情况
当数据量为:2^10
当数据量为:2^20
当数据量为:2^25
当数据量为:2^28
测试代码
int length = (int)Math.Pow(2,28);
List<int> arrs = new List<int>(length);
Random r = new Random();
Stopwatch stopwatch = new Stopwatch();
Console.WriteLine($"******************开始生成数据 {length}********************");
stopwatch.Restart();
for (int i = 0; i < length; i++)
{
arrs.Add(r.Next());
}
stopwatch.Stop();
int min = arrs[0];
int max = arrs[0];
stopwatch.Restart();
foreach (var item in arrs)
{
if (item>max)
{
max = item;
}
if (item<min)
{
min = item;
}
}
stopwatch.Stop();
Console.WriteLine($"*****************排序方案1 Foreach 耗时:{stopwatch.ElapsedMilliseconds}ms max:{max} min:{min} ********************");
stopwatch.Restart();
int min2 = arrs.Min();
int max2 = arrs.Max();
stopwatch.Stop();
Console.WriteLine($"*****************排序方案2 Linq 耗时:{stopwatch.ElapsedMilliseconds}ms max:{max2} min:{min2} ********************");
stopwatch.Restart();
int min3 = arrs.AsParallel().Min();
int max3 = arrs.AsParallel().Max();
stopwatch.Stop();
Console.WriteLine($"*****************排序方案3 Linq AsParallel 耗时:{stopwatch.ElapsedMilliseconds}ms max:{max3} min:{min3} ********************");