Необходимо пополнять какую-нибудь коллекцию парами string, int и после заполнения сортировать по убыванию int. Попробовал разные варианты в прилагаемом коде. Только сомнения в том, верно ли написал тест: код содержит OrderByDescending(pair => pair.Value, не будет ли отложенного выполнения уже после вывода результата?
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
namespace Сравнение_скорости_словаря_и_списка
{
class Program
{
public class myReverserClass : IComparer
{
int IComparer.Compare(Object x, Object y)
{
return ((new CaseInsensitiveComparer()).Compare(x,y ));
}
}
public class myReverserClass1 : IComparer
{
int IComparer.Compare(object x, object y)
{
StrInt x1 = (StrInt)x;StrInt y1 = (StrInt)y;
return ((new CaseInsensitiveComparer()).Compare(y1.ii,x1.ii ));
}
}
class StrInt
{
public string str;
public int ii;
public StrInt(string str, int ii)
{ this.str = str; this.ii = ii; }
}
static void Main(string[] args)
{
Random rnd = new Random();
int c1 = 10000, c2 = 50;
Stopwatch sw = new Stopwatch();
//1
sw.Start();
Dictionary<string, int>[] ardic = new Dictionary<string, int>[c1];
for (int i = 0; i < ardic.Length; i++)
{
var dic = new Dictionary<string, int>(c2);
ardic[i] = dic;
for (int j = 0; j < c2; j++)
{
int value = rnd.Next();
dic.Add(j.ToString(), value);
}
}
sw.Stop(); TimeSpan tsp = new TimeSpan(); tsp += sw.Elapsed;
Console.WriteLine(tsp);
sw.Restart();
Dictionary<string, int>[] ardicsort = new Dictionary<string, int>[c1];
for (int i = 0; i < ardic.Length; i++)
{
ardicsort[i] = ardic[i].OrderByDescending(pair => pair.Value).ToDictionary(pair => pair.Key, pair => pair.Value);
}
tsp = new TimeSpan(); tsp += sw.Elapsed;
Console.WriteLine(tsp);
//2
sw.Restart();
List<(string, int)>[] arl = new List<(string, int)>[c1];
for (int i = 0; i < arl.Length; i++)
{
var l = new List<(string, int)>(c2);
arl[i] = l;
for (int j = 0; j < c2; j++)
{
int value = rnd.Next();
l.Add((j.ToString(), value));
}
}
tsp = new TimeSpan(); tsp += sw.Elapsed;
Console.WriteLine(tsp);
sw.Restart();
List<(string, int)>[] arlsort = new List<(string, int)>[c1];
for (int i = 0; i < arl.Length; i++)
{
arlsort[i] = arl[i].OrderByDescending(pair => pair.Item2).ToList();
}
tsp = new TimeSpan(); tsp += sw.Elapsed;
Console.WriteLine(tsp);
//3 на всякий случай проверяю не string,int а int,int
sw.Restart();
List< int[]>[] arlar = new List< int[]>[c1];
for (int i = 0; i < arlar.Length; i++)
{
var l = new List< int[]>(c2);
arlar[i] = l;
for (int j = 0; j < c2; j++)
{
int value = rnd.Next();
l.Add(new[] { j, value});
}
}
tsp = new TimeSpan(); tsp += sw.Elapsed;
Console.WriteLine(tsp);
sw.Restart();
List< int[]>[] arlarsort = new List<int[]>[c1];
for (int i = 0; i < arlar.Length; i++)
{
arlarsort[i] = arlar[i].OrderByDescending(pair => pair[1]).ToList();
}
tsp = new TimeSpan(); tsp += sw.Elapsed;
Console.WriteLine(tsp);
//4
sw.Restart();
List<(string, int)>[] arl1 = new List<(string, int)>[c1];
for (int i = 0; i < arl1.Length; i++)
{
var l = new List<(string, int)>(c2);
arl1[i] = l;
for (int j = 0; j < c2; j++)
{
int value = rnd.Next();
l.Add((j.ToString(), value));
}
}
(string, int)[][] arart = new (string, int)[arl1.Length][];
for (int i = 0; i < arl1.Length; i++)
{ arart[i] = arl1[i].ToArray(); }
tsp = new TimeSpan(); tsp += sw.Elapsed;
Console.WriteLine(tsp);
sw.Restart();
(string, int)[][] arartsort = new (string, int)[arart.Length][];
for (int i = 0; i < arart.Length; i++)
{
arartsort[i] = arl1[i].OrderByDescending(pair => pair.Item2).ToArray();
}
tsp = new TimeSpan(); tsp += sw.Elapsed;
Console.WriteLine(tsp);
//5
sw.Restart();
List<StrInt>[] arlStrInt = new List<StrInt>[c1];
for (int i = 0; i < arlStrInt.Length; i++)
{
var l = new List<StrInt>(c2);
arlStrInt[i] = l;
for (int j = 0; j < c2; j++)
{
int value = rnd.Next();
l.Add(new StrInt(j.ToString(), value));
}
}
StrInt[][] ararStrInt = new StrInt[arlStrInt.Length][];
for (int i = 0; i < arlStrInt.Length; i++)
{ ararStrInt[i] = arlStrInt[i].ToArray(); }
tsp = new TimeSpan(); tsp += sw.Elapsed;
Console.WriteLine(tsp);
sw.Restart();
IComparer myComparer1 = new myReverserClass1();
for (int i = 0; i < ararStrInt.Length; i++)
{
StrInt[] si = ararStrInt[i];
Array.Sort(si,myComparer1);
}
tsp = new TimeSpan(); tsp += sw.Elapsed;
Console.WriteLine(tsp);
//6
sw.Restart();
List<string>[] arl21 = new List<string>[c1];
List<int>[] arl22 = new List<int>[c1];
for (int i = 0; i < arl21.Length; i++)
{
var l1 = new List<string>(c2); arl21[i] = l1;
var l2 = new List<int>(c2); arl22[i] = l2;
for (int j = 0; j < c2; j++)
{
int value = rnd.Next();
l1.Add(j.ToString()); l2.Add(value);
}
}
string[][] arars1 = new string[arl21.Length][];
int[][] arari2 = new int[arl22.Length][];
tsp = new TimeSpan(); tsp += sw.Elapsed;
Console.WriteLine(tsp);
sw.Restart();
IComparer myComparer = new myReverserClass();
for (int i = 0; i < arars1.Length; i++)
{
string[] ars = arl21[i].ToArray();//arars1[i]
int[] ari = arl22[i].ToArray();//arari2[i]
Array.Sort(ars,ari , myComparer);
arars1[i] = ars; arari2[i] = ari;
}
tsp = new TimeSpan(); tsp += sw.Elapsed;
Console.WriteLine(tsp);
{ }
}
}
}