blob: 9bc11c763900d4cdc7db7cec7b9aca4f60953872 [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
tfarinaf168b862014-06-19 12:32:29 -07008#include "Benchmark.h"
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +00009#include "SkRandom.h"
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +000010#include "SkString.h"
tfarinaf168b862014-06-19 12:32:29 -070011#include "SkTSort.h"
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +000012
bungeman60e0fee2015-08-26 05:15:46 -070013#include <stdlib.h>
14
reed@google.comeebdd962013-01-23 14:52:12 +000015static const int N = 1000;
16
mtklein@google.comb3526272013-09-17 19:37:34 +000017static void rand_proc(int array[N]) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000018 SkRandom rand;
mtklein@google.comb3526272013-09-17 19:37:34 +000019 for (int i = 0; i < N; ++i) {
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +000020 array[i] = rand.nextS();
21 }
22}
23
mtklein@google.comb3526272013-09-17 19:37:34 +000024static void randN_proc(int array[N]) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000025 SkRandom rand;
reed@google.comeebdd962013-01-23 14:52:12 +000026 int mod = N / 10;
mtklein@google.comb3526272013-09-17 19:37:34 +000027 for (int i = 0; i < N; ++i) {
reed@google.comeebdd962013-01-23 14:52:12 +000028 array[i] = rand.nextU() % mod;
29 }
30}
31
mtklein@google.comb3526272013-09-17 19:37:34 +000032static void forward_proc(int array[N]) {
33 for (int i = 0; i < N; ++i) {
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +000034 array[i] = i;
35 }
36}
37
mtklein@google.comb3526272013-09-17 19:37:34 +000038static void backward_proc(int array[N]) {
39 for (int i = 0; i < N; ++i) {
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +000040 array[i] = -i;
41 }
42}
43
mtklein@google.comb3526272013-09-17 19:37:34 +000044static void same_proc(int array[N]) {
45 for (int i = 0; i < N; ++i) {
46 array[i] = N;
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +000047 }
48}
49
mtklein@google.comb3526272013-09-17 19:37:34 +000050typedef void (*SortProc)(int array[N]);
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +000051
52enum Type {
reed@google.comeebdd962013-01-23 14:52:12 +000053 kRand, kRandN, kFore, kBack, kSame
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +000054};
55
56static const struct {
57 const char* fName;
58 SortProc fProc;
59} gRec[] = {
reed@google.comeebdd962013-01-23 14:52:12 +000060 { "rand", rand_proc },
61 { "rand10", randN_proc },
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +000062 { "forward", forward_proc },
63 { "backward", backward_proc },
64 { "repeated", same_proc },
65};
66
mtklein@google.comb3526272013-09-17 19:37:34 +000067static void skqsort_sort(int array[N]) {
68 // End is inclusive for SkTQSort!
69 SkTQSort<int>(array, array + N - 1);
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +000070}
71
mtklein@google.comb3526272013-09-17 19:37:34 +000072static void skheap_sort(int array[N]) {
73 SkTHeapSort<int>(array, N);
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +000074}
75
76extern "C" {
77 static int int_compare(const void* a, const void* b) {
78 const int ai = *(const int*)a;
79 const int bi = *(const int*)b;
80 return ai < bi ? -1 : (ai > bi);
81 }
82}
83
mtklein@google.comb3526272013-09-17 19:37:34 +000084static void qsort_sort(int array[N]) {
85 qsort(array, N, sizeof(int), int_compare);
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +000086}
87
88enum SortType {
89 kSKQSort, kSKHeap, kQSort
90};
91
92static const struct {
93 const char* fName;
94 SortProc fProc;
95} gSorts[] = {
96 { "skqsort", skqsort_sort },
97 { "skheap", skheap_sort },
98 { "qsort", qsort_sort },
99};
100
tfarinaf168b862014-06-19 12:32:29 -0700101class SortBench : public Benchmark {
mtklein@google.comb3526272013-09-17 19:37:34 +0000102 SkString fName;
103 const Type fType;
104 const SortProc fSortProc;
105 SkAutoTMalloc<int> fUnsorted;
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +0000106
107public:
mtklein@google.comb3526272013-09-17 19:37:34 +0000108 SortBench(Type t, SortType s) : fType(t), fSortProc(gSorts[s].fProc) {
mtklein@google.comb3526272013-09-17 19:37:34 +0000109 fName.printf("sort_%s_%s", gSorts[s].fName, gRec[t].fName);
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +0000110 }
111
mtklein36352bf2015-03-25 18:17:31 -0700112 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000113 return backend == kNonRendering_Backend;
114 }
115
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +0000116protected:
mtklein36352bf2015-03-25 18:17:31 -0700117 const char* onGetName() override {
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +0000118 return fName.c_str();
119 }
120
mtklein@google.comb3526272013-09-17 19:37:34 +0000121 // Delayed initialization only done if onDraw will be called.
joshualitt8a6697a2015-09-30 12:11:07 -0700122 void onDelayedSetup() override {
mtklein@google.comb3526272013-09-17 19:37:34 +0000123 fUnsorted.reset(N);
124 gRec[fType].fProc(fUnsorted.get());
125 }
126
mtkleina1ebeb22015-10-01 09:43:39 -0700127 void onDraw(int loops, SkCanvas*) override {
mtklein@google.comb3526272013-09-17 19:37:34 +0000128 SkAutoTMalloc<int> sorted(N);
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000129 for (int i = 0; i < loops; i++) {
mtklein@google.comb3526272013-09-17 19:37:34 +0000130 memcpy(sorted.get(), fUnsorted.get(), N*sizeof(int));
131 fSortProc(sorted.get());
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +0000132#ifdef SK_DEBUG
mtklein@google.comb3526272013-09-17 19:37:34 +0000133 for (int j = 1; j < N; ++j) {
134 SkASSERT(sorted[j - 1] <= sorted[j]);
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +0000135 }
136#endif
137 }
138 }
139
140private:
tfarinaf168b862014-06-19 12:32:29 -0700141 typedef Benchmark INHERITED;
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +0000142};
143
144///////////////////////////////////////////////////////////////////////////////
145
tfarinaf168b862014-06-19 12:32:29 -0700146static Benchmark* NewSkQSort(Type t) {
mtklein@google.comb3526272013-09-17 19:37:34 +0000147 return new SortBench(t, kSKQSort);
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +0000148}
tfarinaf168b862014-06-19 12:32:29 -0700149static Benchmark* NewSkHeap(Type t) {
mtklein@google.comb3526272013-09-17 19:37:34 +0000150 return new SortBench(t, kSKHeap);
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +0000151}
tfarinaf168b862014-06-19 12:32:29 -0700152static Benchmark* NewQSort(Type t) {
mtklein@google.comb3526272013-09-17 19:37:34 +0000153 return new SortBench(t, kQSort);
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +0000154}
155
mtklein@google.com410e6e82013-09-13 19:52:27 +0000156DEF_BENCH( return NewSkQSort(kRand); )
157DEF_BENCH( return NewSkHeap(kRand); )
158DEF_BENCH( return NewQSort(kRand); )
reed@google.comeebdd962013-01-23 14:52:12 +0000159
mtklein@google.com410e6e82013-09-13 19:52:27 +0000160DEF_BENCH( return NewSkQSort(kRandN); )
161DEF_BENCH( return NewSkHeap(kRandN); )
162DEF_BENCH( return NewQSort(kRandN); )
reed@google.comeebdd962013-01-23 14:52:12 +0000163
mtklein@google.com410e6e82013-09-13 19:52:27 +0000164DEF_BENCH( return NewSkQSort(kFore); )
165DEF_BENCH( return NewSkHeap(kFore); )
166DEF_BENCH( return NewQSort(kFore); )
reed@google.comeebdd962013-01-23 14:52:12 +0000167
mtklein@google.com410e6e82013-09-13 19:52:27 +0000168DEF_BENCH( return NewSkQSort(kBack); )
169DEF_BENCH( return NewSkHeap(kBack); )
170DEF_BENCH( return NewQSort(kBack); )
reed@google.comeebdd962013-01-23 14:52:12 +0000171
mtklein@google.com410e6e82013-09-13 19:52:27 +0000172DEF_BENCH( return NewSkQSort(kSame); )
173DEF_BENCH( return NewSkHeap(kSame); )
174DEF_BENCH( return NewQSort(kSame); )