blob: 6fb3a71bf9816646189cbf8d7c4575e4d45054e3 [file] [log] [blame]
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +00001/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "SkBenchmark.h"
9#include "SkRandom.h"
10#include "SkTSort.h"
11#include "SkString.h"
12
reed@google.comeebdd962013-01-23 14:52:12 +000013static const int N = 1000;
14
mtklein@google.comb3526272013-09-17 19:37:34 +000015static void rand_proc(int array[N]) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000016 SkRandom rand;
mtklein@google.comb3526272013-09-17 19:37:34 +000017 for (int i = 0; i < N; ++i) {
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +000018 array[i] = rand.nextS();
19 }
20}
21
mtklein@google.comb3526272013-09-17 19:37:34 +000022static void randN_proc(int array[N]) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000023 SkRandom rand;
reed@google.comeebdd962013-01-23 14:52:12 +000024 int mod = N / 10;
mtklein@google.comb3526272013-09-17 19:37:34 +000025 for (int i = 0; i < N; ++i) {
reed@google.comeebdd962013-01-23 14:52:12 +000026 array[i] = rand.nextU() % mod;
27 }
28}
29
mtklein@google.comb3526272013-09-17 19:37:34 +000030static void forward_proc(int array[N]) {
31 for (int i = 0; i < N; ++i) {
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +000032 array[i] = i;
33 }
34}
35
mtklein@google.comb3526272013-09-17 19:37:34 +000036static void backward_proc(int array[N]) {
37 for (int i = 0; i < N; ++i) {
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +000038 array[i] = -i;
39 }
40}
41
mtklein@google.comb3526272013-09-17 19:37:34 +000042static void same_proc(int array[N]) {
43 for (int i = 0; i < N; ++i) {
44 array[i] = N;
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +000045 }
46}
47
mtklein@google.comb3526272013-09-17 19:37:34 +000048typedef void (*SortProc)(int array[N]);
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +000049
50enum Type {
reed@google.comeebdd962013-01-23 14:52:12 +000051 kRand, kRandN, kFore, kBack, kSame
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +000052};
53
54static const struct {
55 const char* fName;
56 SortProc fProc;
57} gRec[] = {
reed@google.comeebdd962013-01-23 14:52:12 +000058 { "rand", rand_proc },
59 { "rand10", randN_proc },
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +000060 { "forward", forward_proc },
61 { "backward", backward_proc },
62 { "repeated", same_proc },
63};
64
mtklein@google.comb3526272013-09-17 19:37:34 +000065static void skqsort_sort(int array[N]) {
66 // End is inclusive for SkTQSort!
67 SkTQSort<int>(array, array + N - 1);
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +000068}
69
mtklein@google.comb3526272013-09-17 19:37:34 +000070static void skheap_sort(int array[N]) {
71 SkTHeapSort<int>(array, N);
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +000072}
73
74extern "C" {
75 static int int_compare(const void* a, const void* b) {
76 const int ai = *(const int*)a;
77 const int bi = *(const int*)b;
78 return ai < bi ? -1 : (ai > bi);
79 }
80}
81
mtklein@google.comb3526272013-09-17 19:37:34 +000082static void qsort_sort(int array[N]) {
83 qsort(array, N, sizeof(int), int_compare);
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +000084}
85
86enum SortType {
87 kSKQSort, kSKHeap, kQSort
88};
89
90static const struct {
91 const char* fName;
92 SortProc fProc;
93} gSorts[] = {
94 { "skqsort", skqsort_sort },
95 { "skheap", skheap_sort },
96 { "qsort", qsort_sort },
97};
98
99class SortBench : public SkBenchmark {
mtklein@google.comb3526272013-09-17 19:37:34 +0000100 SkString fName;
101 const Type fType;
102 const SortProc fSortProc;
103 SkAutoTMalloc<int> fUnsorted;
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +0000104
105public:
mtklein@google.comb3526272013-09-17 19:37:34 +0000106 SortBench(Type t, SortType s) : fType(t), fSortProc(gSorts[s].fProc) {
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +0000107 fIsRendering = false;
mtklein@google.comb3526272013-09-17 19:37:34 +0000108 fName.printf("sort_%s_%s", gSorts[s].fName, gRec[t].fName);
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +0000109 }
110
111protected:
112 virtual const char* onGetName() SK_OVERRIDE {
113 return fName.c_str();
114 }
115
mtklein@google.comb3526272013-09-17 19:37:34 +0000116 // Delayed initialization only done if onDraw will be called.
117 virtual void onPreDraw() SK_OVERRIDE {
118 fUnsorted.reset(N);
119 gRec[fType].fProc(fUnsorted.get());
120 }
121
122 virtual void onDraw(SkCanvas*) SK_OVERRIDE {
123 SkAutoTMalloc<int> sorted(N);
mtklein@google.comc2897432013-09-10 19:23:38 +0000124 for (int i = 0; i < this->getLoops(); i++) {
mtklein@google.comb3526272013-09-17 19:37:34 +0000125 memcpy(sorted.get(), fUnsorted.get(), N*sizeof(int));
126 fSortProc(sorted.get());
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +0000127#ifdef SK_DEBUG
mtklein@google.comb3526272013-09-17 19:37:34 +0000128 for (int j = 1; j < N; ++j) {
129 SkASSERT(sorted[j - 1] <= sorted[j]);
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +0000130 }
131#endif
132 }
133 }
134
135private:
136 typedef SkBenchmark INHERITED;
137};
138
139///////////////////////////////////////////////////////////////////////////////
140
mtklein@google.com410e6e82013-09-13 19:52:27 +0000141static SkBenchmark* NewSkQSort(Type t) {
mtklein@google.comb3526272013-09-17 19:37:34 +0000142 return new SortBench(t, kSKQSort);
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +0000143}
mtklein@google.com410e6e82013-09-13 19:52:27 +0000144static SkBenchmark* NewSkHeap(Type t) {
mtklein@google.comb3526272013-09-17 19:37:34 +0000145 return new SortBench(t, kSKHeap);
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +0000146}
mtklein@google.com410e6e82013-09-13 19:52:27 +0000147static SkBenchmark* NewQSort(Type t) {
mtklein@google.comb3526272013-09-17 19:37:34 +0000148 return new SortBench(t, kQSort);
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +0000149}
150
mtklein@google.com410e6e82013-09-13 19:52:27 +0000151DEF_BENCH( return NewSkQSort(kRand); )
152DEF_BENCH( return NewSkHeap(kRand); )
153DEF_BENCH( return NewQSort(kRand); )
reed@google.comeebdd962013-01-23 14:52:12 +0000154
mtklein@google.com410e6e82013-09-13 19:52:27 +0000155DEF_BENCH( return NewSkQSort(kRandN); )
156DEF_BENCH( return NewSkHeap(kRandN); )
157DEF_BENCH( return NewQSort(kRandN); )
reed@google.comeebdd962013-01-23 14:52:12 +0000158
mtklein@google.com410e6e82013-09-13 19:52:27 +0000159DEF_BENCH( return NewSkQSort(kFore); )
160DEF_BENCH( return NewSkHeap(kFore); )
161DEF_BENCH( return NewQSort(kFore); )
reed@google.comeebdd962013-01-23 14:52:12 +0000162
mtklein@google.com410e6e82013-09-13 19:52:27 +0000163DEF_BENCH( return NewSkQSort(kBack); )
164DEF_BENCH( return NewSkHeap(kBack); )
165DEF_BENCH( return NewQSort(kBack); )
reed@google.comeebdd962013-01-23 14:52:12 +0000166
mtklein@google.com410e6e82013-09-13 19:52:27 +0000167DEF_BENCH( return NewSkQSort(kSame); )
168DEF_BENCH( return NewSkHeap(kSame); )
169DEF_BENCH( return NewQSort(kSame); )