blob: f8bb6862733a8963a56bb389192328f32e916b9b [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
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +000015static void rand_proc(int array[], int count) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000016 SkRandom rand;
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +000017 for (int i = 0; i < count; ++i) {
18 array[i] = rand.nextS();
19 }
20}
21
reed@google.comeebdd962013-01-23 14:52:12 +000022static void randN_proc(int array[], int count) {
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;
25 for (int i = 0; i < count; ++i) {
26 array[i] = rand.nextU() % mod;
27 }
28}
29
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +000030static void forward_proc(int array[], int count) {
31 for (int i = 0; i < count; ++i) {
32 array[i] = i;
33 }
34}
35
36static void backward_proc(int array[], int count) {
37 for (int i = 0; i < count; ++i) {
38 array[i] = -i;
39 }
40}
41
42static void same_proc(int array[], int count) {
43 for (int i = 0; i < count; ++i) {
44 array[i] = count;
45 }
46}
47
48typedef void (*SortProc)(int array[], int count);
49
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
65static void skqsort_sort(int array[], int count) {
66 SkTQSort<int>(array, array + count);
67}
68
69static void skheap_sort(int array[], int count) {
70 SkTHeapSort<int>(array, count);
71}
72
73extern "C" {
74 static int int_compare(const void* a, const void* b) {
75 const int ai = *(const int*)a;
76 const int bi = *(const int*)b;
77 return ai < bi ? -1 : (ai > bi);
78 }
79}
80
81static void qsort_sort(int array[], int count) {
82 qsort(array, count, sizeof(int), int_compare);
83}
84
85enum SortType {
86 kSKQSort, kSKHeap, kQSort
87};
88
89static const struct {
90 const char* fName;
91 SortProc fProc;
92} gSorts[] = {
93 { "skqsort", skqsort_sort },
94 { "skheap", skheap_sort },
95 { "qsort", qsort_sort },
96};
97
98class SortBench : public SkBenchmark {
99 SkString fName;
100 enum { MAX = 100000 };
101 int fUnsorted[MAX];
102 int fSorted[MAX];
103 int fCount;
104 SortProc fSortProc;
105
106public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000107 SortBench(Type t, int n, SortType s) {
reed@google.comeebdd962013-01-23 14:52:12 +0000108 if (n > MAX) {
109 n = MAX;
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +0000110 }
111 fName.printf("sort_%s_%s", gSorts[s].fName, gRec[t].fName);
reed@google.comeebdd962013-01-23 14:52:12 +0000112 fCount = n;
113 gRec[t].fProc(fUnsorted, n);
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +0000114 fSortProc = gSorts[s].fProc;
115 fIsRendering = false;
116 }
117
118protected:
119 virtual const char* onGetName() SK_OVERRIDE {
120 return fName.c_str();
121 }
122
sugoi@google.com77472f02013-03-05 18:50:01 +0000123 virtual void onDraw(SkCanvas*) {
mtklein@google.comc2897432013-09-10 19:23:38 +0000124 for (int i = 0; i < this->getLoops(); i++) {
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +0000125 memcpy(fSorted, fUnsorted, fCount * sizeof(int));
126 fSortProc(fSorted, fCount);
127#ifdef SK_DEBUG
128 for (int j = 1; j < fCount; ++j) {
129 SkASSERT(fSorted[j - 1] <= fSorted[j]);
130 }
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) {
142 return new SortBench(t, N, kSKQSort);
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +0000143}
mtklein@google.com410e6e82013-09-13 19:52:27 +0000144static SkBenchmark* NewSkHeap(Type t) {
145 return new SortBench(t, N, kSKHeap);
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +0000146}
mtklein@google.com410e6e82013-09-13 19:52:27 +0000147static SkBenchmark* NewQSort(Type t) {
148 return new SortBench(t, N, 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); )