【android studio】android 中使用 System.arraycopy()测试

更新时间:2017-05-30    来源:php常用代码    手机版     字体:

【www.bbyears.com--php常用代码】

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

下面是PC上的测试代码:

 代码如下 private static final int SIZE_OF_ARRAY = 10000000;
 private static long time;
 
 public static void main(String[] args) {
 
             Integer [] sourceArray = new Integer[SIZE_OF_ARRAY];
             Integer [] destinationArray = new Integer[SIZE_OF_ARRAY];
             fillArray(sourceArray);
 
             startBenchmark();
             naiveCopy(sourceArray,destinationArray);
             stopBenchmark();
 
             startBenchmark();
             System.arraycopy(sourceArray, 0, destinationArray, 0,
                       sourceArray.length);
             stopBenchmark();
         }
 
         private static void naiveCopy(Integer [] src, Integer [] dst) {
             for (int i = 0; i < src.length; i++) {
                 dst[i]=src[i];
             }
 
         }
 
         private static void fillArray(Integer [] src) {
             for (int i = 0; i < src.length; i++) {
                 src[i]=i;
             }
 
         }
 
         private static void startBenchmark() {
             time = System.currentTimeMillis();
         }
 
         private static void stopBenchmark() {
             time = System.currentTimeMillis() - time;
             System.out.println( "time="+time);
  }

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

Naive algorithm – 14 ms

System.arraycopy(); – 6 ms.

Arraycopy 快了一半的时间。

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

 代码如下 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()要比普通的数组拷贝快的更多

 

本文来源:http://www.bbyears.com/jiaocheng/33014.html