using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace UrlMatchSiteNameModule
{
class Program
{
static void Main(string[] args)
{
const int COUNT = 1000000;
HashSet<int> hashSetOfInts = new HashSet<int>();
Stopwatch stopWatch = new Stopwatch();
for (int i = 0; i < COUNT; i++)
{
hashSetOfInts.Add(i);
}
stopWatch.Start();
for (int i = 0; i < COUNT; i++)
{
hashSetOfInts.Contains(i);
}
stopWatch.Stop();
Console.WriteLine(stopWatch.Elapsed);
stopWatch.Reset();
Dictionary<string, string> domainMap = new Dictionary<string, string>();
for (int i = 0; i < COUNT; i++)
{
domainMap.Add(i.ToString(), i.ToString());
}
stopWatch.Start();
string value;
for (int i = 0; i < COUNT; i++)
{
domainMap.TryGetValue(i.ToString(), out value);
}
stopWatch.Stop();
Console.WriteLine(stopWatch.Elapsed);
stopWatch.Reset();
List<int> listOfInts = new List<int>();
for (int i = 0; i < COUNT; i++)
{
listOfInts.Add(i);
}
stopWatch.Start();
for (int i = 0; i < COUNT; i++)
{
listOfInts.Contains(i);
}
stopWatch.Stop();
Console.WriteLine(stopWatch.Elapsed);
Console.Read();
}
}
}
HashSet 、 Dictionary 、List性能对比
最新推荐文章于 2024-07-17 22:55:05 发布