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