1/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/**
18 * Array write speed test.
19 */
20public class Main {
21    /** whether to report times */
22    static boolean timing = false;
23
24    static final int STORAGE_SIZE = 128*1024;
25    static int[] mStorage = new int[STORAGE_SIZE];
26
27    static public void report(long start, long end) {
28        if (! timing) {
29            return;
30        }
31
32        System.out.println("Finished in " + ((end - start) / 1000000.0)
33            + " msec");
34    }
35
36    static void writeArray(int val) {
37        for (int i = STORAGE_SIZE-1; i >= 0; i--)
38            mStorage[i] = val;
39    }
40
41    static void writeTest() {
42        long start, end;
43
44        writeArray(0);  // touch all the memory
45
46        System.out.println("Running writeTest...");
47        start = System.nanoTime();
48        for (int i = 1; i < 20; i++)
49            writeArray(i);
50        end = System.nanoTime();
51
52        report(start, end);
53    }
54
55    static void copyTest() {
56        long start, end;
57
58        // touch once
59        System.arraycopy(mStorage, 0, mStorage,
60            STORAGE_SIZE/2, STORAGE_SIZE/2);
61
62        System.out.println("Running copyTest...");
63        start = System.nanoTime();
64        for (int i = 1; i < 35; i++) {
65            System.arraycopy(mStorage, 0, mStorage,
66                STORAGE_SIZE/2, STORAGE_SIZE/2);
67        }
68        end = System.nanoTime();
69
70        report(start, end);
71    }
72
73    public static void array_028() {
74        writeTest();
75        copyTest();
76        System.out.println("Done!");
77    }
78
79    public static void main(String[] args) {
80        if ((args.length >= 1) && args[0].equals("--timing")) {
81            timing = true;
82        }
83        array_028();
84    }
85}
86