孙广东 2016.5.24
集合的影响有哪些?
难道应该完全停止使用字典集合么?
总之,几毫秒的缓慢循环都会让玩家发疯!!!(集合都太慢了) 其实在游戏开发中经常会遇到对集合数据的排序:
在应用程序中我们管理相关对象通过以下两种方式之一:
- 通过创建对象数组(创建就不变大小)
- 通过创建对象的集合(可以动态改变大小)
每一种集合都有特定的用途,都有自己的优点和缺点。
List<GameObject> myListOfGameObjects = new List<GameObject>();
Dictionary<int, String> myDictionary = new Dictionary<int, String>();
新建一个脚本 作为测试:
public class GenericCollectionsTest : MonoBehaviour
{
#region PUBLIC_DECLARATIONS
public int numberOfIterations = 10000000;
#endregion
#region PRIVATE_DECLARATIONS
private Stopwatch stopWatch;
private List<int> intList; // 整数列表
private Dictionary<int,int> intDictionary; // 一本字典,键和值为整数。
private int[] intArray; // 一个整数数组
#endregion
#region UNITY_CALLBACKS
void Start()
{
stopWatch = new Stopwatch();
intArray = new int[numberOfIterations];
intList = new List<int>();
intDictionary = new Dictionary<int, int>();
AddFakeValuesInArray(numberOfIterations);
AddFakeValuesInList(numberOfIterations);
AddFakeValuesInDictionay(numberOfI