zoukankan      html  css  js  c++  java
  • android:应用性能优化SparseArray

    HashMap是java里比较常用的一个集合类,我比较习惯用来缓存一些处理后的结果。最近在做一个Android项目,在代码中定义这样一个变量,实例化时,Eclipse却给出了一个 performance 警告。

    意思就是说用SparseArray 来替代,以获取更好性能。老实说,对SparseArray并不熟悉,第一感觉应该是Android提供的一个类。按住Ctrl点击进入SparseArray的源码,果不其然,确定是Android提供的一个工具类。

    单纯从字面上来理解,SparseArray指的是稀疏数组(Sparse array),所谓稀疏数组就是数组中大部分的内容值都未被使用(或都为零),在数组中仅有少部分的空间使用。因此造成内存空间的浪费,为了节省内存空间,并且不影响数组中原有的内容值,我们可以采用一种压缩的方式来表示稀疏数组的内容。

    假设有一个9*7的数组,其内容如下:

    在此数组中,共有63个空间,但却只使用了5个元素,造成58个元素空间的浪费。以下我们就使用稀疏数组重新来定义这个数组:

    其中在稀疏数组中第一部分所记录的是原数组的列数和行数以及元素使用的个数、第二部分所记录的是原数组中元素的位置和内容。经过压缩之后,原来需要声明大小为63的数组,而使用压缩后,只需要声明大小为6*3的数组,仅需18个存储空间。

    继续阅读SparseArray的源码,从构造方法我们可以看出,它和一般的List一样,可以预先设置容器大小,默认的大小是10:

    1. public SparseArray() {
    2. this(10);
    3. }
    4. public SparseArray(int initialCapacity) {
    5. initialCapacity = ArrayUtils.idealIntArraySize(initialCapacity);
    6. mKeys = new int[initialCapacity];
    7. mValues = new Object[initialCapacity];
    8. mSize = 0;
    9. }

    再来看看它对数据的”增删改查”。

    它有两个方法可以添加键值对:

    1. public void put(int key, E value) {}
    2. public void append(int key, E value){}

    有四个方法可以执行删除操作:

    1. public void delete(int key) {}
    2. public void remove(int key) {} //直接调用的delete(int key)
    3. public void removeAt(int index){}
    4. public void clear(){}

    修改数据起初以为只有setValueAt(int index, E value)可以修改数据,但后来发现put(int key, E value)也可以修改数据,我们查看put(int key, E value)的源码可知,在put数据之前,会先查找要put的数据是否已经存在,如果存在就是修改,不存在就添加。

    1. public void put(int key, E value) {
    2. int i = binarySearch(mKeys, 0, mSize, key);
    3. if (i > = 0) {
    4. mValues[i] = value;
    5. } else {
    6. i = ~i;
    7. if (i < mSize &amp;&amp; mValues[i] == DELETED) {
    8. mKeys[i] = key;
    9. mValues[i] = value;
    10. return;
    11. }
    12. if (mGarbage &amp;&amp; mSize > = mKeys.length) {
    13. gc();
    14. // Search again because indices may have changed.
    15. i = ~binarySearch(mKeys, 0, mSize, key);
    16. }
    17. …………

    所以,修改数据实际也有两种方法:

    1. public void put(int key, E value)
    2. public void setValueAt(int index, E value)

    最后再来看看如何查找数据。有两个方法可以查询取值:

    1. public E get(int key)
    2. public E get(int key, E valueIfKeyNotFound)

    其中get(int key)也只是调用了 get(int key,E valueIfKeyNotFound),最后一个从传参的变量名就能看出,传入的是找不到的时候返回的值.get(int key)当找不到的时候,默认返回null。

    查看第几个位置的键:public int keyAt(int index)
    有一点需要注意的是,查看键所在位置,由于是采用二分法查找键的位置,所以找不到时返回小于0的数值,而不是返回-1。返回的负值是表示它在找不到时所在的位置。

    查看第几个位置的值:
    public E valueAt(int index)
    查看值所在位置,没有的话返回-1:
    public int indexOfValue(E value)
    最后,发现其核心就是折半查找函数(binarySearch),算法设计的很不错。

    1. private static int binarySearch(int[] a, int start, int len, int key) {
    2. int high = start + len, low = start - 1, guess;
    3. while (high - low > 1) {
    4. guess = (high + low) / 2;
    5. if (a[guess] < key)
    6. low = guess;
    7. else
    8. high = guess;
    9. }
    10. if (high == start + len)
    11. return ~(start + len);
    12. else if (a[high] == key)
    13. return high;
    14. else
    15. return ~high;
    16. }

    相应的也有SparseBooleanArray,用来取代HashMap <integer, boolean="">,SparseIntArray用来取代HashMap <integer, integer="">,大家有兴趣的可以研究。

    总结:
    SparseArray是android里为 <interger,object>这样的Hashmap而专门写的类,目的是提高效率,其核心是折半查找函数(binarySearch)。在Android中,当我们需要定义
    HashMap <Integer, E> hashMap = new HashMap <Integer, E> ();
    时,我们可以使用如下的方式来取得更好的性能.
    SparseArray <E> sparseArray = new SparseArray <E> ();

  • 相关阅读:
    [LeetCode] 146. LRU Cache(LRU 缓存)
    [LeetCode] 55. Jump Game(跳跃游戏)
    [LeetCode] 33. Search in Rotated Sorted Array(搜索旋转有序数组)
    [LeetCode] 19. Remove Nth Node From End of List(从单链表中移除倒数第 n 个节点)
    [LeetCode] 79. Word Search(单词查找)
    [LeetCode] 322. Coin Change(换硬币)
    [LeetCode] 34. Find First and Last Position of Elements in Sorted Array(在有序数组中寻找某个元素第一次和最后一次出现的位置)
    第04组 Alpha冲刺(2/6)
    第04组 Alpha冲刺(1/6)
    第04组 团队Git现场编程实战
  • 原文地址:https://www.cnblogs.com/zgqys1980/p/5109828.html
Copyright © 2011-2022 走看看