Unity的优化系列3 - 理解数组、 列表和字典的详细

孙广东  2016.5.24

计算机生成了可选文字:
      

集合的影响有哪些?

难道应该完全停止使用字典集合么?

总之,几毫秒的缓慢循环都会让玩家发疯!!!(集合都太慢了)   其实在游戏开发中经常会遇到对集合数据的排序:

在应用程序中我们管理相关对象通过以下两种方式之一:

  1. 通过创建对象数组(创建就不变大小)
  2. 通过创建对象的集合(可以动态改变大小)


每一种集合都有特定的用途,都有自己的优点和缺点。

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
  • 4
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值