using System;
using System.Collections.Generic;
class Example
{
static void Main(string[] args)
{
Example example = new Example();
CarProfiler.BeginSample();
//example.ListTest();
example.DictionaryTest();
CarProfiler.EndSample();
CarProfiler.LogElapsedTicks();
CarDebug.Info(GC.GetTotalMemory(false));
Console.Read();
}
private List<List<int>> mListList = new List<List<int>>();
private List<Dictionary<int, int>> mDictionaryList = new List<Dictionary<int, int>>();
public void ListTest()
{
for (int i = 0; i < 10000; ++i)
{
mListList.Add(new List<int>());
}
}
public void DictionaryTest()
{
for (int i = 0; i < 10000; ++i)
{
mDictionaryList.Add(new Dictionary<int, int>());
}
}
}
运行在windows10 .net framework 4.6.1环境下
测试结果:生成1万个List的时间为1314Tick(0.1毫秒多点),内存占用为2140236字节(2.04MB多一点)。生成1万个Dictionary的时间为2087Tick(0.2毫秒多点),内存占用为2402276字节(2.29MB多一点)
结论:Dictionary的生成时间要长一点,Dictionary的内存占用略大。