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