1 /*
2  * Copyright (C) 2015 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 import com.google.caliper.SimpleBenchmark;
18 
19 public class ScopedPrimitiveArrayBenchmark extends SimpleBenchmark {
20   // Measure adds the first and last element of the array by using ScopedPrimitiveArray.
measureByteArray(int reps, byte[] arr)21   static native long measureByteArray(int reps, byte[] arr);
measureShortArray(int reps, short[] arr)22   static native long measureShortArray(int reps, short[] arr);
measureIntArray(int reps, int[] arr)23   static native long measureIntArray(int reps, int[] arr);
measureLongArray(int reps, long[] arr)24   static native long measureLongArray(int reps, long[] arr);
25 
26   static final int smallLength = 16;
27   static final int mediumLength = 256;
28   static final int largeLength = 8096;
29   static byte[] smallBytes = new byte[smallLength];
30   static byte[] mediumBytes = new byte[mediumLength];
31   static byte[] largeBytes = new byte[largeLength];
32   static short[] smallShorts = new short[smallLength];
33   static short[] mediumShorts = new short[mediumLength];
34   static short[] largeShorts = new short[largeLength];
35   static int[] smallInts = new int[smallLength];
36   static int[] mediumInts = new int[mediumLength];
37   static int[] largeInts = new int[largeLength];
38   static long[] smallLongs = new long[smallLength];
39   static long[] mediumLongs = new long[mediumLength];
40   static long[] largeLongs = new long[largeLength];
41 
timeSmallBytes(int reps)42   public void timeSmallBytes(int reps) {
43     measureByteArray(reps, smallBytes);
44   }
45 
timeMediumBytes(int reps)46   public void timeMediumBytes(int reps) {
47     measureByteArray(reps, mediumBytes);
48   }
49 
timeLargeBytes(int reps)50   public void timeLargeBytes(int reps) {
51     measureByteArray(reps, largeBytes);
52   }
53 
timeSmallShorts(int reps)54   public void timeSmallShorts(int reps) {
55     measureShortArray(reps, smallShorts);
56   }
57 
timeMediumShorts(int reps)58   public void timeMediumShorts(int reps) {
59     measureShortArray(reps, mediumShorts);
60   }
61 
timeLargeShorts(int reps)62   public void timeLargeShorts(int reps) {
63     measureShortArray(reps, largeShorts);
64   }
65 
timeSmallInts(int reps)66   public void timeSmallInts(int reps) {
67     measureIntArray(reps, smallInts);
68   }
69 
timeMediumInts(int reps)70   public void timeMediumInts(int reps) {
71     measureIntArray(reps, mediumInts);
72   }
73 
timeLargeInts(int reps)74   public void timeLargeInts(int reps) {
75     measureIntArray(reps, largeInts);
76   }
77 
timeSmallLongs(int reps)78   public void timeSmallLongs(int reps) {
79     measureLongArray(reps, smallLongs);
80   }
81 
timeMediumLongs(int reps)82   public void timeMediumLongs(int reps) {
83     measureLongArray(reps, mediumLongs);
84   }
85 
timeLargeLongs(int reps)86   public void timeLargeLongs(int reps) {
87     measureLongArray(reps, largeLongs);
88   }
89 
90   {
91     System.loadLibrary("artbenchmark");
92   }
93 }
94