Android Performance Performance boost

Source: Internet
Author: User

1. Frequently changing strings are used StringBuilder, then each change with the Append method. You should not create a new String every time.

2. Use the static final variable.

3. It ' s reasonable to follow common object-oriented programming practices and has getters and setters in the public Inter Face, but within a class should always access fields directly. Only for public interface with getters and setters. For ordinary classes, you should use the public property directly.

4. The method of array traversal.

Static classFoo {intMsplat;} Foo[] Marray= ... Public voidZero () {intsum = 0;  for(inti = 0; i < marray.length; ++i) {sum+=Marray[i].msplat; }} Public voidOne () {intsum = 0; Foo[] LocalArray=Marray; intLen =localarray.length;  for(inti = 0; i < Len; ++i) {sum+=Localarray[i].msplat; }} Public voidBoth () {intsum = 0;  for(Foo a:marray) {sum+=A.msplat; }}

The last function is the best, and the second to the bottom is a little. The first function is absolutely wrong.

5. Can avoid the overhead by declaring fields and methods accessed by inner classes to a package access, rather tha n Private access. Unfortunately this means the fields can be accessed directly by other classes in the same package so you shouldn ' t use th is in the public API. Try to avoid using the private inner class. Visibility should be visible to the package as much as possible (nothing added) rather than private.

6. You can use int as much as possible without floating-point numbers, because the cost is higher. Double and float have the same speed.

7. Call HashMap 的方法比Map 快。

Interface Aspect ***********************************************************

8. Do not nest using the weight property. Because there are many calculations to be made. Especially for components with frequently changing interfaces such as ListView和 GridView

9. Using a composite control is more efficient than using multiple controls separately. A custom component, for example, contains a TextView and a ImageView.

10. Avoid using too many previously nested layout

11. Use <include/> or<merge/> 来重用 layout。

12. For the ListView, use ViewHolder to hold the control to avoid repeating the Findviewbyid.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.