Android Code performance Optimization Tips (i)

Source: Internet
Author: User

Reprinted from http://blog.csdn.net/leilu2008/article/details/6672979

We all know that the JIT performance of Android 2.2 has been improved substantially, but for older programs to improve Java Execution Efficiency There are many language features, for the Java 1.5 There will be significant improvements after that. The following example is from the SDK:

[Java]View Plaincopy
  1. Static class Foo {
  2. int Msplat;
  3. }
  4. foo[] Marray = ...
  5. The execution and performance of the Static class Foo above, we compare three methods zero, one and two.
  6. public void Zero () { ///Most people may simply write this directly
  7. int sum = 0;
  8. for (int i = 0; i < marray.length; ++i) {
  9. sum + = Marray[i].msplat;
  10. }
  11. }
  12. Public void One () { //improve performance with local objects
  13. int sum = 0;
  14. foo[] LocalArray = Marray;
  15. int len = localarray.length;
  16. for (int i = 0; i < len; ++i) {
  17. sum + = Localarray[i].msplat;
  18. }
  19. }
  20. Public Void () { ///recommended method, with new syntax features in Java 1.5 can greatly improve performance
  21. int sum = 0;
  22. for (Foo A:marray) {
  23. sum + = A.msplat;
  24. }
  25. }



Zero () slowest , one() faster, and both() the fastest, I hope these have some help for you.

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.