blob: c19ebae0c8ff21554ec22ceb44b46fd099c84292 [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:
reed@google.comeebdd962013-01-23 14:52:12 +0000107 SortBench(void* param, Type t, int n, SortType s) : INHERITED(param) {
108 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*) {
reed@google.comeebdd962013-01-23 14:52:12 +0000124 int n = SkBENCHLOOP(200);
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +0000125 for (int i = 0; i < n; i++) {
126 memcpy(fSorted, fUnsorted, fCount * sizeof(int));
127 fSortProc(fSorted, fCount);
128#ifdef SK_DEBUG
129 for (int j = 1; j < fCount; ++j) {
130 SkASSERT(fSorted[j - 1] <= fSorted[j]);
131 }
132#endif
133 }
134 }
135
136private:
137 typedef SkBenchmark INHERITED;
138};
139
140///////////////////////////////////////////////////////////////////////////////
141
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +0000142static SkBenchmark* NewSkQSort(void* param, Type t) {
143 return new SortBench(param, t, N, kSKQSort);
144}
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +0000145static SkBenchmark* NewSkHeap(void* param, Type t) {
146 return new SortBench(param, t, N, kSKHeap);
147}
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +0000148static SkBenchmark* NewQSort(void* param, Type t) {
149 return new SortBench(param, t, N, kQSort);
150}
151
reed@google.comeebdd962013-01-23 14:52:12 +0000152DEF_BENCH( return NewSkQSort(p, kRand); )
153DEF_BENCH( return NewSkHeap(p, kRand); )
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +0000154DEF_BENCH( return NewQSort(p, kRand); )
reed@google.comeebdd962013-01-23 14:52:12 +0000155
156DEF_BENCH( return NewSkQSort(p, kRandN); )
157DEF_BENCH( return NewSkHeap(p, kRandN); )
158DEF_BENCH( return NewQSort(p, kRandN); )
159
160DEF_BENCH( return NewSkQSort(p, kFore); )
161DEF_BENCH( return NewSkHeap(p, kFore); )
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +0000162DEF_BENCH( return NewQSort(p, kFore); )
reed@google.comeebdd962013-01-23 14:52:12 +0000163
164DEF_BENCH( return NewSkQSort(p, kBack); )
165DEF_BENCH( return NewSkHeap(p, kBack); )
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +0000166DEF_BENCH( return NewQSort(p, kBack); )
reed@google.comeebdd962013-01-23 14:52:12 +0000167
168DEF_BENCH( return NewSkQSort(p, kSame); )
169DEF_BENCH( return NewSkHeap(p, kSame); )
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +0000170DEF_BENCH( return NewQSort(p, kSame); )