17fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin/*
27fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin * Copyright (C) 2009 Google Inc.
37fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin *
47fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin * Licensed under the Apache License, Version 2.0 (the "License");
57fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin * you may not use this file except in compliance with the License.
67fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin * You may obtain a copy of the License at
77fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin *
87fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin * http://www.apache.org/licenses/LICENSE-2.0
97fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin *
107fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin * Unless required by applicable law or agreed to in writing, software
117fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin * distributed under the License is distributed on an "AS IS" BASIS,
127fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
137fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin * See the License for the specific language governing permissions and
147fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin * limitations under the License.
157fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin */
167fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin
177fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffinpackage examples;
187fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin
19e236301e5fc778bffe1748ed80d7936e6c807012Paul Duffinimport com.google.caliper.BeforeExperiment;
20e236301e5fc778bffe1748ed80d7936e6c807012Paul Duffinimport com.google.caliper.Benchmark;
217fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffinimport com.google.caliper.Param;
22e236301e5fc778bffe1748ed80d7936e6c807012Paul Duffin
237fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffinimport java.util.Arrays;
247fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffinimport java.util.Random;
257fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin
267fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin/**
277fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin * Measures sorting on different distributions of integers.
287fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin */
29e236301e5fc778bffe1748ed80d7936e6c807012Paul Duffinpublic class ArraySortBenchmark {
307fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin
317fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin  @Param({"10", "100", "1000", "10000"}) private int length;
327fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin
337fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin  @Param private Distribution distribution;
347fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin
357fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin  private int[] values;
367fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin  private int[] copy;
377fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin
38e236301e5fc778bffe1748ed80d7936e6c807012Paul Duffin  @BeforeExperiment void setUp() throws Exception {
397fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin    values = distribution.create(length);
407fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin    copy = new int[length];
417fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin  }
427fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin
43e236301e5fc778bffe1748ed80d7936e6c807012Paul Duffin  @Benchmark void sort(int reps) {
447fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin    for (int i = 0; i < reps; i++) {
457fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin      System.arraycopy(values, 0, copy, 0, values.length);
467fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin      Arrays.sort(copy);
477fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin    }
487fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin  }
497fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin
507fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin  public enum Distribution {
517fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin    SAWTOOTH {
527fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin      @Override
537fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin      int[] create(int length) {
547fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin        int[] result = new int[length];
557fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin        for (int i = 0; i < length; i += 5) {
567fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin          result[i] = 0;
577fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin          result[i + 1] = 1;
587fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin          result[i + 2] = 2;
597fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin          result[i + 3] = 3;
607fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin          result[i + 4] = 4;
617fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin        }
627fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin        return result;
637fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin      }
647fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin    },
657fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin    INCREASING {
667fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin      @Override
677fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin      int[] create(int length) {
687fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin        int[] result = new int[length];
697fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin        for (int i = 0; i < length; i++) {
707fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin          result[i] = i;
717fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin        }
727fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin        return result;
737fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin      }
747fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin    },
757fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin    DECREASING {
767fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin      @Override
777fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin      int[] create(int length) {
787fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin        int[] result = new int[length];
797fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin        for (int i = 0; i < length; i++) {
807fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin          result[i] = length - i;
817fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin        }
827fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin        return result;
837fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin      }
847fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin    },
857fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin    RANDOM {
867fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin      @Override
877fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin      int[] create(int length) {
887fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin        Random random = new Random();
897fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin        int[] result = new int[length];
907fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin        for (int i = 0; i < length; i++) {
917fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin          result[i] = random.nextInt();
927fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin        }
937fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin        return result;
947fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin      }
957fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin    };
967fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin
977fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin    abstract int[] create(int length);
987fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin  }
997fc0b45feb5ac6191547bb0a175a718cb41ec4f9Paul Duffin}
100