Style or performance: "new ContentValues ​​()" VS "contentValues.clear ()"

I'm never sure, so I usually find myself inconsistent.

Which is more efficient:

  • Re-create ContentValues

    many times
  • Create ContentValues

    once and then call clear()

    each time before using it

Example (1) :

while (condition) {
  ContentValues values = new ContentValues();

  // Add to values

  // Create an operation
}

// Execute a batch of operations

      

Example (2) :

ContentValues values = new ContentValues();

while (condition) {
  values.clear();

  // Add to values

  // Create an operation
}

// Execute a batch of operations

      

Assuming a large enough number of iterations to make a difference.

+3


source to share


2 answers


A typical use case for ContentValues ​​looks like this:

// Defines an object to contain the new values to insert
ContentValues mNewValues = new ContentValues();

/*
* Sets the values of each column and inserts the word. The arguments to the "put"
* method are "column name" and "value"
*/
mNewValues.put(UserDictionary.Words.APP_ID, "example.user");
mNewValues.put(UserDictionary.Words.LOCALE, "en_US");
mNewValues.put(UserDictionary.Words.WORD, "insert");
mNewValues.put(UserDictionary.Words.FREQUENCY, "100");

      



Now you how you use it for your requirements is up to you depending on what your logic requires from hasMore()

0


source


I would go with values.clear () every time. Now that I think about it, if the set of keys is fixed, that is, you add values ​​for the same set of keys in each iteration, you also don't need to clear (). You will be overwriting it for each iteration.



0


source







All Articles