CollectionsBenchmark.java revision fa080b83161fa8f2c538009b312d074dd17016bc
1/*
2 * Copyright (C) 2014 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
17package benchmarks.regression;
18
19import com.google.caliper.Param;
20import com.google.caliper.SimpleBenchmark;
21import java.util.ArrayList;
22import java.util.Collections;
23import java.util.List;
24import java.util.Random;
25import java.util.Vector;
26
27public class CollectionsBenchmark extends SimpleBenchmark {
28    @Param({"4", "16", "64", "256", "1024"})
29    private int arrayListLength;
30
31    public void timeSort_arrayList(int nreps) {
32        List<Integer> input = buildList(arrayListLength, true /* use array list */);
33        for (int i = 0; i < nreps; ++i) {
34            Collections.sort(input);
35        }
36    }
37
38    public void timeSort_vector(int nreps) {
39        List<Integer> input = buildList(arrayListLength, false /* use array list */);
40        for (int i = 0; i < nreps; ++i) {
41            Collections.sort(input);
42        }
43    }
44
45    private static List<Integer> buildList(int arrayListLength, boolean useArrayList) {
46        Random random = new Random();
47        random.setSeed(0);
48        List<Integer> list = useArrayList ? new ArrayList<Integer>(arrayListLength) :
49                new Vector<Integer>(arrayListLength);
50        for (int i = 0; i < arrayListLength; ++i) {
51            list.add(random.nextInt());
52        }
53        return list;
54    }
55}
56