代码改变世界

Android 快速提示: 使用 System.arraycopy()(译文)

2011-12-18 21:52  MudooT  阅读(3307)  评论(4编辑  收藏  举报
来自本人的wordpress博客:

http://www.naitiz.com/index.php/android-quick-tip-use-system-arraycopy_125.html

本文为译文,原文地址 : 

http://www.aviyehuda.com/2011/06/android-quick-tip-use-system-arraycopy/

众所周知,使用JNI的方法System.arraycopy() 是一种有效数组拷贝的方法,因为它采用native的方式去调用内存,但是这是否同样适用于Android平台呢?如果是这样,那么到底是更有效到什么程度呢?

为了回答这个问题,我做了一个简单的测试运行在PC机和android的activity里面。

下面是PC上的测试代码: 

 1 private static final int SIZE_OF_ARRAY = 10000000;
 2 private static long time;
 3 
 4 public static void main(String[] args) {
 5 
 6             Integer [] sourceArray = new Integer[SIZE_OF_ARRAY];
 7             Integer [] destinationArray = new Integer[SIZE_OF_ARRAY];
 8             fillArray(sourceArray);
 9 
10             startBenchmark();
11             naiveCopy(sourceArray,destinationArray);
12             stopBenchmark();
13 
14             startBenchmark();
15             System.arraycopy(sourceArray, 0, destinationArray, 0,
16                       sourceArray.length);
17             stopBenchmark();
18         }
19 
20         private static void naiveCopy(Integer [] src, Integer [] dst) {
21             for (int i = 0; i < src.length; i++) {
22                 dst[i]=src[i];
23             }
24 
25         }
26 
27         private static void fillArray(Integer [] src) {
28             for (int i = 0; i < src.length; i++) {
29                 src[i]=i;
30             }
31 
32         }
33 
34         private static void startBenchmark() {
35             time = System.currentTimeMillis();
36         }
37 
38         private static void stopBenchmark() {
39             time = System.currentTimeMillis() - time;
40             System.out.println( "time="+time);
41  }

PC机测试结果如下:(java 7, 8GB 内存, CPU intel i5)

Naive algorithm – 14 ms

System.arraycopy(); – 6 ms.

Arraycopy 快了一半的时间。

同样的在android上面测试,代码如下: 

 1 public class ArrayCopyTestActivity extends Activity {        

 2 private static final int SIZE_OF_ARRAY = 1000000;
 3     private long time;
 4 
 5     /** Called when the activity is first created. */
 6     @Override
 7     public void onCreate(Bundle savedInstanceState) {
 8         super.onCreate(savedInstanceState);
 9         setContentView(R.layout.main);
10 
11         Integer [] sourceArray = new Integer[SIZE_OF_ARRAY];
12         Integer [] dst = new Integer[SIZE_OF_ARRAY];
13         fillArray(sourceArray);
14 
15         startBenchmark();
16         naiveCopy(sourceArray,dst);
17         stopBenchmark();
18 
19         startBenchmark();
20         System.arraycopy(sourceArray, 0, dst, 0, sourceArray.length);
21         stopBenchmark();
22     }
23 
24     private void naiveCopy(Integer [] src, Integer [] dst) {
25         for (int i = 0; i < src.length; i++) {
26             dst[i]=src[i];
27         }
28 
29     }
30 
31     private void fillArray(Integer [] src) {
32         for (int i = 0; i < src.length; i++) {
33             src[i]=i;
34         }
35 
36     }
37 
38     private void startBenchmark() {
39         time = System.currentTimeMillis();
40     }
41 
42     private void stopBenchmark() {
43         time = System.currentTimeMillis() - time;
44         Log.d("array copy test", "time="+time);
45 
46     }
47 }

 *注意,我将数组的长度从1000W缩短至100W ,这是因为android平台上允许的内存有限。

 

nexus 1 上得出的运行结果为:

Naive algorithm – 182 ms

System.arraycopy(); – 12 ms.

这个事实意味着在android上使用System.arraycopy()要比普通的数组拷贝快的更多。

总的来说,尽量在android上面去使用System.arraycopy()来代替数组复制吧。