blob: 6933ee2636791afc067709eae2c4c33024cbcb1d [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "bench/Benchmark.h"
9#include "include/core/SkString.h"
10#include "include/utils/SkRandom.h"
11#include "src/core/SkTSort.h"
Hal Canary8a001442018-09-19 11:31:27 -040012
Mike Klein2372a332018-05-31 14:37:39 -040013#include <algorithm>
bungeman60e0fee2015-08-26 05:15:46 -070014#include <stdlib.h>
15
reed@google.comeebdd962013-01-23 14:52:12 +000016static const int N = 1000;
17
mtklein@google.comb3526272013-09-17 19:37:34 +000018static void rand_proc(int array[N]) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000019 SkRandom rand;
mtklein@google.comb3526272013-09-17 19:37:34 +000020 for (int i = 0; i < N; ++i) {
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +000021 array[i] = rand.nextS();
22 }
23}
24
mtklein@google.comb3526272013-09-17 19:37:34 +000025static void randN_proc(int array[N]) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000026 SkRandom rand;
reed@google.comeebdd962013-01-23 14:52:12 +000027 int mod = N / 10;
mtklein@google.comb3526272013-09-17 19:37:34 +000028 for (int i = 0; i < N; ++i) {
reed@google.comeebdd962013-01-23 14:52:12 +000029 array[i] = rand.nextU() % mod;
30 }
31}
32
mtklein@google.comb3526272013-09-17 19:37:34 +000033static void forward_proc(int array[N]) {
34 for (int i = 0; i < N; ++i) {
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +000035 array[i] = i;
36 }
37}
38
mtklein@google.comb3526272013-09-17 19:37:34 +000039static void backward_proc(int array[N]) {
40 for (int i = 0; i < N; ++i) {
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +000041 array[i] = -i;
42 }
43}
44
mtklein@google.comb3526272013-09-17 19:37:34 +000045static void same_proc(int array[N]) {
46 for (int i = 0; i < N; ++i) {
47 array[i] = N;
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +000048 }
49}
50
mtklein@google.comb3526272013-09-17 19:37:34 +000051typedef void (*SortProc)(int array[N]);
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +000052
53enum Type {
reed@google.comeebdd962013-01-23 14:52:12 +000054 kRand, kRandN, kFore, kBack, kSame
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +000055};
56
57static const struct {
58 const char* fName;
59 SortProc fProc;
60} gRec[] = {
reed@google.comeebdd962013-01-23 14:52:12 +000061 { "rand", rand_proc },
62 { "rand10", randN_proc },
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +000063 { "forward", forward_proc },
64 { "backward", backward_proc },
65 { "repeated", same_proc },
66};
67
mtklein@google.comb3526272013-09-17 19:37:34 +000068static void skqsort_sort(int array[N]) {
69 // End is inclusive for SkTQSort!
70 SkTQSort<int>(array, array + N - 1);
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +000071}
72
mtklein@google.comb3526272013-09-17 19:37:34 +000073static void skheap_sort(int array[N]) {
74 SkTHeapSort<int>(array, N);
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +000075}
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
mtklein@google.comb3526272013-09-17 19:37:34 +000085static void qsort_sort(int array[N]) {
86 qsort(array, N, sizeof(int), int_compare);
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +000087}
88
Mike Klein2372a332018-05-31 14:37:39 -040089static void stdsort_sort(int array[N]) {
90 std::sort(array, array+N);
91}
92
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +000093enum SortType {
Mike Klein2372a332018-05-31 14:37:39 -040094 kSKQSort, kSKHeap, kQSort, kStdSort,
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +000095};
96
97static const struct {
98 const char* fName;
99 SortProc fProc;
100} gSorts[] = {
101 { "skqsort", skqsort_sort },
Mike Klein2372a332018-05-31 14:37:39 -0400102 { "skheap", skheap_sort },
103 { "qsort", qsort_sort },
104 { "stdsort", stdsort_sort },
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +0000105};
106
tfarinaf168b862014-06-19 12:32:29 -0700107class SortBench : public Benchmark {
mtklein@google.comb3526272013-09-17 19:37:34 +0000108 SkString fName;
109 const Type fType;
110 const SortProc fSortProc;
111 SkAutoTMalloc<int> fUnsorted;
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +0000112
113public:
mtklein@google.comb3526272013-09-17 19:37:34 +0000114 SortBench(Type t, SortType s) : fType(t), fSortProc(gSorts[s].fProc) {
mtklein@google.comb3526272013-09-17 19:37:34 +0000115 fName.printf("sort_%s_%s", gSorts[s].fName, gRec[t].fName);
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +0000116 }
117
mtklein36352bf2015-03-25 18:17:31 -0700118 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000119 return backend == kNonRendering_Backend;
120 }
121
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +0000122protected:
mtklein36352bf2015-03-25 18:17:31 -0700123 const char* onGetName() override {
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +0000124 return fName.c_str();
125 }
126
mtklein@google.comb3526272013-09-17 19:37:34 +0000127 // Delayed initialization only done if onDraw will be called.
joshualitt8a6697a2015-09-30 12:11:07 -0700128 void onDelayedSetup() override {
mtklein@google.comb3526272013-09-17 19:37:34 +0000129 fUnsorted.reset(N);
130 gRec[fType].fProc(fUnsorted.get());
131 }
132
mtkleina1ebeb22015-10-01 09:43:39 -0700133 void onDraw(int loops, SkCanvas*) override {
mtklein@google.comb3526272013-09-17 19:37:34 +0000134 SkAutoTMalloc<int> sorted(N);
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000135 for (int i = 0; i < loops; i++) {
mtklein@google.comb3526272013-09-17 19:37:34 +0000136 memcpy(sorted.get(), fUnsorted.get(), N*sizeof(int));
137 fSortProc(sorted.get());
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +0000138#ifdef SK_DEBUG
mtklein@google.comb3526272013-09-17 19:37:34 +0000139 for (int j = 1; j < N; ++j) {
140 SkASSERT(sorted[j - 1] <= sorted[j]);
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +0000141 }
142#endif
143 }
144 }
145
146private:
tfarinaf168b862014-06-19 12:32:29 -0700147 typedef Benchmark INHERITED;
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +0000148};
149
150///////////////////////////////////////////////////////////////////////////////
151
tfarinaf168b862014-06-19 12:32:29 -0700152static Benchmark* NewSkQSort(Type t) {
mtklein@google.comb3526272013-09-17 19:37:34 +0000153 return new SortBench(t, kSKQSort);
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +0000154}
tfarinaf168b862014-06-19 12:32:29 -0700155static Benchmark* NewSkHeap(Type t) {
mtklein@google.comb3526272013-09-17 19:37:34 +0000156 return new SortBench(t, kSKHeap);
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +0000157}
tfarinaf168b862014-06-19 12:32:29 -0700158static Benchmark* NewQSort(Type t) {
mtklein@google.comb3526272013-09-17 19:37:34 +0000159 return new SortBench(t, kQSort);
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +0000160}
Mike Klein2372a332018-05-31 14:37:39 -0400161static Benchmark* NewStdSort(Type t) {
162 return new SortBench(t, kStdSort);
163}
mike@reedtribe.orga3f1c8e2013-01-23 02:23:40 +0000164
mtklein@google.com410e6e82013-09-13 19:52:27 +0000165DEF_BENCH( return NewSkQSort(kRand); )
166DEF_BENCH( return NewSkHeap(kRand); )
167DEF_BENCH( return NewQSort(kRand); )
Mike Klein2372a332018-05-31 14:37:39 -0400168DEF_BENCH( return NewStdSort(kRand); )
reed@google.comeebdd962013-01-23 14:52:12 +0000169
mtklein@google.com410e6e82013-09-13 19:52:27 +0000170DEF_BENCH( return NewSkQSort(kRandN); )
171DEF_BENCH( return NewSkHeap(kRandN); )
172DEF_BENCH( return NewQSort(kRandN); )
Mike Klein2372a332018-05-31 14:37:39 -0400173DEF_BENCH( return NewStdSort(kRandN); )
reed@google.comeebdd962013-01-23 14:52:12 +0000174
mtklein@google.com410e6e82013-09-13 19:52:27 +0000175DEF_BENCH( return NewSkQSort(kFore); )
176DEF_BENCH( return NewSkHeap(kFore); )
177DEF_BENCH( return NewQSort(kFore); )
Mike Klein2372a332018-05-31 14:37:39 -0400178DEF_BENCH( return NewStdSort(kFore); )
reed@google.comeebdd962013-01-23 14:52:12 +0000179
mtklein@google.com410e6e82013-09-13 19:52:27 +0000180DEF_BENCH( return NewSkQSort(kBack); )
181DEF_BENCH( return NewSkHeap(kBack); )
182DEF_BENCH( return NewQSort(kBack); )
Mike Klein2372a332018-05-31 14:37:39 -0400183DEF_BENCH( return NewStdSort(kBack); )
reed@google.comeebdd962013-01-23 14:52:12 +0000184
mtklein@google.com410e6e82013-09-13 19:52:27 +0000185DEF_BENCH( return NewSkQSort(kSame); )
186DEF_BENCH( return NewSkHeap(kSame); )
187DEF_BENCH( return NewQSort(kSame); )
Mike Klein2372a332018-05-31 14:37:39 -0400188DEF_BENCH( return NewStdSort(kSame); )