日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android高效编程注意事项

發布時間:2025/3/14 Android 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android高效编程注意事项 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
原文:http://kuikui.javaeye.com/blog/256665

最近用 Android開發了幾個模塊,感覺有點慢,后來好好看了相關優化Android代碼的知識,優化之后,感覺快了很多。在這里與大家分享一下,下面只是說 的一些很基礎有很重要的知識,你想要編寫運行速度很快、 占用內存少的代碼可能有點幫助。

概述

There are two basic rules for resource-constrained systems

Don't do work that you don't need to do.

Don't allocate memory if you can avoid it.

All the tips below follow from these two basic tenets.

知識點

1,?Avoid Creating Objects

能不使用包裝類就不使用包裝類。

盡量使用StringBuffer來處理字符串

盡量使用一維數組代替多維數組

2,?Use Native Methods

盡量使用系統提供的接口方法,因為系統提供的接口方法使用C編寫的,比自己用Java編寫的效率高

3,?Prefer Virtual Over Interface

多使用接口的具體實現類。

<1>,Map myMap1 = new HashMap();

<2>,HashMap myMap2 = new HashMap();

兩者比較結果:第一種是一向大家 比較推崇的,因為他對以后的維護成本低,但是接口方法的調用比實現類方法的調用更耗時。

4,?Prefer Static Over Virtual

多使用靜態的方法和屬性

5,?Avoid Internal Getters/Setters

避免使用C++C形式的(i=this.getCounter())這樣子的代碼

6,?Cache Field Lookups

訪問對象的屬性比訪問本地變量花費時間多。

??Accessing object fields is much slower than accessing local variables.

?Instead of writing:

for (int i = 0; i < this.mCount; i++)

?????dumpItem(this.mItems[i]);

You should write:

???int count = this.mCount;

???Item[] items = this.mItems;

???for (int i = 0; i < count; i++)

?????dumpItems(items[i]);

7,?Declare Constants Final

聲明一些final類型的常量

static int intVal = 42;

static String strVal = "Hello, world!";

可以寫成如下:

static final int intVal = 42;???????????????????????????????????????????????????????????????????????????????????????????

static final String strVal = "Hello, world!";

?

8,?Use Enhanced For Loop Syntax With Caution

謹慎使用增強的for循環,因為它創建多余臨時變量。

public class Foo {

???int mSplat;

???static Foo mArray[] = new Foo[27];

?

???public static void zero() {

???????int sum = 0;

???????for (int i = 0; i < mArray.length; i++) {

???????????sum += mArray[i].mSplat;

???????}

???}

?

???public static void one() {

???????int sum = 0;

???????Foo[] localArray = mArray;

???????int len = localArray.length;

?

???????for (int i = 0; i < len; i++) {

???????????sum += localArray[i].mSplat;

???????}

???}

?

???public static void two() {

???????int sum = 0;

???????for (Foo a: mArray) {

???????????sum += a.mSplat;

???????}

???}

}

zero()返回兩次靜態字段、每次 循環的時候都要請求數組的長度

one()將所有的屬性存放到本地,避免查找。

two()使用jdk1.5以上版本的增強for循環,這是有編譯器拷貝數組的引用和長度到本地這在主循環體會產生額外的本地裝載和存儲,這跟one()相比,比其運行時間長一小點,同時也比one()4byte的存儲空間u

To summarize all that a bit more clearly: enhanced for loop syntax performs well with arrays, but be cautious when using it with Iterable objects since there is additional object creation.

9,?Avoid Enums

避免使用枚舉。

10,?????????????Use Package Scope with Inner Classes

建議使 用內部類

public class Foo {

???private int mValue;

?

???public void run() {

???????Inner in = new Inner();

???????mValue = 27;

???????in.stuff();

???}

?

???private void doStuff(int value) {

???????System.out.println("Value is " + value);

???}

?

???private class Inner {

???????void stuff() {

???????????Foo.this.doStuff(Foo.this.mValue);

???????}

???}

}

11,?????????????Avoid Float

盡量避免使用float類型

12,?????????????Some Sample Performance Numbers

一些常用操作的占用時間相對比 較:

操作???????????????????????????????????????????????????????????????時間

Add a local variable????????????????????????????????????????????????1

Add a member variable???????????????????????????????????????????????????????4

Call String.length()???????????????????????????????????????????????????????????????????5

Call empty static native method??????????????????????????????????????????5

Call empty static method?????????????????????????????????????????????????????12

Call empty virtual method???????????????????????????????????????????????????12.5

Call empty interface method?????????????????????????????????????????????15

Call Iterator:next() on a HashMap????????????????????????????????????165

Call put() on a HashMap?????????????????????????????????????????????????????????600

Inflate 1 View from XML????????????????????????????????????????????????????????????????22,000

Inflate 1 LinearLayout containing 1 TextView????????????????25,000

Inflate 1 LinearLayout containing 6 View objects?????????100,000

Inflate 1 LinearLayout containing 6 TextView objects???135,000

Launch an empty activity?????????????????????????????????????????????????????3,000,000

?這 些時間相對值,值得我們好好的權衡哦,很有幫助。

轉載于:https://www.cnblogs.com/huaping-audio/archive/2010/03/22/1691492.html

總結

以上是生活随笔為你收集整理的Android高效编程注意事项的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。