jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame^] | 1 | // Copyright 2007 The Android Open Source Project |
| 2 | |
| 3 | /** |
| 4 | * Array write speed test. |
| 5 | */ |
| 6 | public class Main { |
| 7 | /** whether to report times */ |
| 8 | static boolean timing = false; |
| 9 | |
| 10 | static final int STORAGE_SIZE = 128*1024; |
| 11 | static int[] mStorage = new int[STORAGE_SIZE]; |
| 12 | |
| 13 | static public void report(long start, long end) { |
| 14 | if (! timing) { |
| 15 | return; |
| 16 | } |
| 17 | |
| 18 | System.out.println("Finished in " + ((end - start) / 1000000.0) |
| 19 | + " msec"); |
| 20 | } |
| 21 | |
| 22 | static void writeArray(int val) { |
| 23 | for (int i = STORAGE_SIZE-1; i >= 0; i--) |
| 24 | mStorage[i] = val; |
| 25 | } |
| 26 | |
| 27 | static void writeTest() { |
| 28 | long start, end; |
| 29 | |
| 30 | writeArray(0); // touch all the memory |
| 31 | |
| 32 | System.out.println("Running writeTest..."); |
| 33 | start = System.nanoTime(); |
| 34 | for (int i = 1; i < 20; i++) |
| 35 | writeArray(i); |
| 36 | end = System.nanoTime(); |
| 37 | |
| 38 | report(start, end); |
| 39 | } |
| 40 | |
| 41 | static void copyTest() { |
| 42 | long start, end; |
| 43 | |
| 44 | // touch once |
| 45 | System.arraycopy(mStorage, 0, mStorage, |
| 46 | STORAGE_SIZE/2, STORAGE_SIZE/2); |
| 47 | |
| 48 | System.out.println("Running copyTest..."); |
| 49 | start = System.nanoTime(); |
| 50 | for (int i = 1; i < 35; i++) { |
| 51 | System.arraycopy(mStorage, 0, mStorage, |
| 52 | STORAGE_SIZE/2, STORAGE_SIZE/2); |
| 53 | } |
| 54 | end = System.nanoTime(); |
| 55 | |
| 56 | report(start, end); |
| 57 | } |
| 58 | |
| 59 | public static void main(String[] args) { |
| 60 | if ((args.length >= 1) && args[0].equals("--timing")) { |
| 61 | timing = true; |
| 62 | } |
| 63 | |
| 64 | writeTest(); |
| 65 | copyTest(); |
| 66 | System.out.println("Done!"); |
| 67 | } |
| 68 | } |