blob: d7d827f441f6132cc28cefeef3b7c370962278e2 [file] [log] [blame]
John Stiles6e9ead92020-07-14 00:13:51 +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 "bench/Benchmark.h"
9#include "include/core/SkString.h"
10#include "include/utils/SkRandom.h"
11#include "src/core/SkTSort.h"
12
13#include <algorithm>
14#include <stdlib.h>
15
16static const int N = 1000;
17
18static void rand_proc(int array[N]) {
19 SkRandom rand;
20 for (int i = 0; i < N; ++i) {
21 array[i] = rand.nextS();
22 }
23}
24
25static void randN_proc(int array[N]) {
26 SkRandom rand;
27 int mod = N / 10;
28 for (int i = 0; i < N; ++i) {
29 array[i] = rand.nextU() % mod;
30 }
31}
32
33static void forward_proc(int array[N]) {
34 for (int i = 0; i < N; ++i) {
35 array[i] = i;
36 }
37}
38
39static void backward_proc(int array[N]) {
40 for (int i = 0; i < N; ++i) {
41 array[i] = -i;
42 }
43}
44
45static void same_proc(int array[N]) {
46 for (int i = 0; i < N; ++i) {
47 array[i] = N;
48 }
49}
50
51typedef void (*SortProc)(int array[N]);
52
53enum Type {
54 kRand, kRandN, kFore, kBack, kSame
55};
56
57static const struct {
58 const char* fName;
59 SortProc fProc;
60} gRec[] = {
61 { "rand", rand_proc },
62 { "rand10", randN_proc },
63 { "forward", forward_proc },
64 { "backward", backward_proc },
65 { "repeated", same_proc },
66};
67
68static void skqsort_sort(int array[N]) {
John Stiles886a9042020-07-14 16:28:33 -040069 SkTQSort<int>(array, array + N);
John Stiles6e9ead92020-07-14 00:13:51 +000070}
71
72static void skheap_sort(int array[N]) {
73 SkTHeapSort<int>(array, N);
74}
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
84static void qsort_sort(int array[N]) {
85 qsort(array, N, sizeof(int), int_compare);
86}
87
88static void stdsort_sort(int array[N]) {
89 std::sort(array, array+N);
90}
91
92enum SortType {
93 kSKQSort, kSKHeap, kQSort, kStdSort,
94};
95
96static const struct {
97 const char* fName;
98 SortProc fProc;
99} gSorts[] = {
100 { "skqsort", skqsort_sort },
101 { "skheap", skheap_sort },
102 { "qsort", qsort_sort },
103 { "stdsort", stdsort_sort },
104};
105
106class SortBench : public Benchmark {
107 SkString fName;
108 const Type fType;
109 const SortProc fSortProc;
110 SkAutoTMalloc<int> fUnsorted;
111
112public:
113 SortBench(Type t, SortType s) : fType(t), fSortProc(gSorts[s].fProc) {
114 fName.printf("sort_%s_%s", gSorts[s].fName, gRec[t].fName);
115 }
116
117 bool isSuitableFor(Backend backend) override {
118 return backend == kNonRendering_Backend;
119 }
120
121protected:
122 const char* onGetName() override {
123 return fName.c_str();
124 }
125
126 // Delayed initialization only done if onDraw will be called.
127 void onDelayedSetup() override {
128 fUnsorted.reset(N);
129 gRec[fType].fProc(fUnsorted.get());
130 }
131
132 void onDraw(int loops, SkCanvas*) override {
133 SkAutoTMalloc<int> sorted(N);
134 for (int i = 0; i < loops; i++) {
135 memcpy(sorted.get(), fUnsorted.get(), N*sizeof(int));
136 fSortProc(sorted.get());
137#ifdef SK_DEBUG
138 for (int j = 1; j < N; ++j) {
139 SkASSERT(sorted[j - 1] <= sorted[j]);
140 }
141#endif
142 }
143 }
144
145private:
146 typedef Benchmark INHERITED;
147};
148
149///////////////////////////////////////////////////////////////////////////////
150
151static Benchmark* NewSkQSort(Type t) {
152 return new SortBench(t, kSKQSort);
153}
154static Benchmark* NewSkHeap(Type t) {
155 return new SortBench(t, kSKHeap);
156}
157static Benchmark* NewQSort(Type t) {
158 return new SortBench(t, kQSort);
159}
160static Benchmark* NewStdSort(Type t) {
161 return new SortBench(t, kStdSort);
162}
163
164DEF_BENCH( return NewSkQSort(kRand); )
165DEF_BENCH( return NewSkHeap(kRand); )
166DEF_BENCH( return NewQSort(kRand); )
167DEF_BENCH( return NewStdSort(kRand); )
168
169DEF_BENCH( return NewSkQSort(kRandN); )
170DEF_BENCH( return NewSkHeap(kRandN); )
171DEF_BENCH( return NewQSort(kRandN); )
172DEF_BENCH( return NewStdSort(kRandN); )
173
174DEF_BENCH( return NewSkQSort(kFore); )
175DEF_BENCH( return NewSkHeap(kFore); )
176DEF_BENCH( return NewQSort(kFore); )
177DEF_BENCH( return NewStdSort(kFore); )
178
179DEF_BENCH( return NewSkQSort(kBack); )
180DEF_BENCH( return NewSkHeap(kBack); )
181DEF_BENCH( return NewQSort(kBack); )
182DEF_BENCH( return NewStdSort(kBack); )
183
184DEF_BENCH( return NewSkQSort(kSame); )
185DEF_BENCH( return NewSkHeap(kSame); )
186DEF_BENCH( return NewQSort(kSame); )
187DEF_BENCH( return NewStdSort(kSame); )