SparseIntArray查找性能

时间:2017-09-17 08:31:21

标签: android

我可以访问值的关键字和索引,我想从SparseIntArray获取。

这意味着我可以使用get(key)valueAt(index)获取值。

我相信后者会是 O(1)复杂性,但我不确定,我不知道get(key)的复杂性。

哪一个更快?

2 个答案:

答案 0 :(得分:3)

要回答您的问题,让我们查看两种实现的源代码:

获取(key)的

<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://unpkg.com/vue@latest/dist/vue.js"></script>
<script src="https://unpkg.com/lodash@4.16.0/lodash.js"></script>
<div id="app">
  <div class="row">
    <div class="col-xs-12" v-if="!laravelData || laravelData.total === 0"><em>No data available.</em></div>
    <div class="col-xs-12">
      <div class="table-scrollable">
        <table class="table table-striped table-bordered table-hover">
          <thead>
            <tr>
              <th>
              ID 
              <i @click="sort('id','asc')" class="fa fa-sort-asc " aria-hidden="true"></i>
              <i @click="sort('id','desc')" class="fa fa-sort-desc" aria-hidden="true"></i>
              </th>
              <th> First Name</th>
              <th> Last Name</th>
              <th> Email </th>
              <th> DOB </th>
            </tr>
          </thead>
          <tbody>
            <tr v-for="data in laravelData.data" :key="data.title">
              <td> <a :href="data.account_link">@{{ data.id }}</a> </td>
              <td> @{{ data.firstName }}</td>
              <td> @{{ data.lastName }} </td>
              <td class="text-right"> @{{ data.email }} </td>
              <td class="text-right"> @{{ data.dob }} </td>
            </tr>
            <tr v-if="!laravelData || laravelData.total === 0">
              <td colspan="13"><em>No data available.</em></td>
            </tr>
          </tbody>
        </table>
      </div>
    </div>
  </div>
</div>

<强> valueAt(指数):

/**
 * Gets the int mapped from the specified key, or <code>0</code>
 * if no such mapping has been made.
 */
public int get(int key) {
    return get(key, 0);
}

/**
 * Gets the int mapped from the specified key, or the specified value
 * if no such mapping has been made.
 */
public int get(int key, int valueIfKeyNotFound) {
    int i = ContainerHelpers.binarySearch(mKeys, mSize, key);

    if (i < 0) {
        return valueIfKeyNotFound;
    } else {
        return mValues[i];
    }
}

此处/** * Given an index in the range <code>0...size()-1</code>, returns * the value from the <code>index</code>th key-value mapping that this * SparseIntArray stores. * * <p>The values corresponding to indices in ascending order are guaranteed * to be associated with keys in ascending order, e.g., * <code>valueAt(0)</code> will return the value associated with the * smallest key and <code>valueAt(size()-1)</code> will return the value * associated with the largest key.</p> */ public int valueAt(int index) { return mValues[index]; } 函数首先搜索键并返回键的值,而get()函数直接返回给定索引的值。

因此,valueAt()显然比valueAt()快。

答案 1 :(得分:1)

来自文档:

valueAt()方法:

   public int valueAt(int index) {
        return mValues[index];
   }

get()方法:

    public int get(int key) {
        return get(key, 0);
    }

    public int get(int key, int valueIfKeyNotFound) {
        int i = ContainerHelpers.binarySearch(mKeys, mSize, key);

        if (i < 0) {
          return valueIfKeyNotFound;
        } else {
          return mValues[i];
      }
    }

正如您所看到的,上面的方法更快方法是valueAt(key),但它并不安全。它可能会产生IndexOutOfBoundException。但是如果没有找到这样的索引,get(key)方法将返回0。