Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 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 | */ |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 16 | |
| 17 | /** |
| 18 | * Array write speed test. |
| 19 | */ |
| 20 | public 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 | |
buzbee | 8fa0fda | 2012-06-27 15:44:52 -0700 | [diff] [blame] | 73 | public static void array_028() { |
| 74 | writeTest(); |
| 75 | copyTest(); |
| 76 | System.out.println("Done!"); |
| 77 | } |
| 78 | |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 79 | public static void main(String[] args) { |
| 80 | if ((args.length >= 1) && args[0].equals("--timing")) { |
| 81 | timing = true; |
| 82 | } |
buzbee | 8fa0fda | 2012-06-27 15:44:52 -0700 | [diff] [blame] | 83 | array_028(); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 84 | } |
| 85 | } |