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 | |
| 8 | #include "SkBenchmark.h" |
| 9 | #include "SkRandom.h" |
| 10 | #include "SkTSort.h" |
| 11 | #include "SkString.h" |
| 12 | |
reed@google.com | eebdd96 | 2013-01-23 14:52:12 +0000 | [diff] [blame^] | 13 | static const int N = 1000; |
| 14 | |
mike@reedtribe.org | a3f1c8e | 2013-01-23 02:23:40 +0000 | [diff] [blame] | 15 | static void rand_proc(int array[], int count) { |
| 16 | SkRandom rand; |
| 17 | for (int i = 0; i < count; ++i) { |
| 18 | array[i] = rand.nextS(); |
| 19 | } |
| 20 | } |
| 21 | |
reed@google.com | eebdd96 | 2013-01-23 14:52:12 +0000 | [diff] [blame^] | 22 | static void randN_proc(int array[], int count) { |
| 23 | SkRandom rand; |
| 24 | int mod = N / 10; |
| 25 | for (int i = 0; i < count; ++i) { |
| 26 | array[i] = rand.nextU() % mod; |
| 27 | } |
| 28 | } |
| 29 | |
mike@reedtribe.org | a3f1c8e | 2013-01-23 02:23:40 +0000 | [diff] [blame] | 30 | static void forward_proc(int array[], int count) { |
| 31 | for (int i = 0; i < count; ++i) { |
| 32 | array[i] = i; |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | static void backward_proc(int array[], int count) { |
| 37 | for (int i = 0; i < count; ++i) { |
| 38 | array[i] = -i; |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | static void same_proc(int array[], int count) { |
| 43 | for (int i = 0; i < count; ++i) { |
| 44 | array[i] = count; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | typedef void (*SortProc)(int array[], int count); |
| 49 | |
| 50 | enum Type { |
reed@google.com | eebdd96 | 2013-01-23 14:52:12 +0000 | [diff] [blame^] | 51 | kRand, kRandN, kFore, kBack, kSame |
mike@reedtribe.org | a3f1c8e | 2013-01-23 02:23:40 +0000 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | static const struct { |
| 55 | const char* fName; |
| 56 | SortProc fProc; |
| 57 | } gRec[] = { |
reed@google.com | eebdd96 | 2013-01-23 14:52:12 +0000 | [diff] [blame^] | 58 | { "rand", rand_proc }, |
| 59 | { "rand10", randN_proc }, |
mike@reedtribe.org | a3f1c8e | 2013-01-23 02:23:40 +0000 | [diff] [blame] | 60 | { "forward", forward_proc }, |
| 61 | { "backward", backward_proc }, |
| 62 | { "repeated", same_proc }, |
| 63 | }; |
| 64 | |
| 65 | static void skqsort_sort(int array[], int count) { |
| 66 | SkTQSort<int>(array, array + count); |
| 67 | } |
| 68 | |
| 69 | static void skheap_sort(int array[], int count) { |
| 70 | SkTHeapSort<int>(array, count); |
| 71 | } |
| 72 | |
| 73 | extern "C" { |
| 74 | static int int_compare(const void* a, const void* b) { |
| 75 | const int ai = *(const int*)a; |
| 76 | const int bi = *(const int*)b; |
| 77 | return ai < bi ? -1 : (ai > bi); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | static void qsort_sort(int array[], int count) { |
| 82 | qsort(array, count, sizeof(int), int_compare); |
| 83 | } |
| 84 | |
| 85 | enum SortType { |
| 86 | kSKQSort, kSKHeap, kQSort |
| 87 | }; |
| 88 | |
| 89 | static const struct { |
| 90 | const char* fName; |
| 91 | SortProc fProc; |
| 92 | } gSorts[] = { |
| 93 | { "skqsort", skqsort_sort }, |
| 94 | { "skheap", skheap_sort }, |
| 95 | { "qsort", qsort_sort }, |
| 96 | }; |
| 97 | |
| 98 | class SortBench : public SkBenchmark { |
| 99 | SkString fName; |
| 100 | enum { MAX = 100000 }; |
| 101 | int fUnsorted[MAX]; |
| 102 | int fSorted[MAX]; |
| 103 | int fCount; |
| 104 | SortProc fSortProc; |
| 105 | |
| 106 | public: |
reed@google.com | eebdd96 | 2013-01-23 14:52:12 +0000 | [diff] [blame^] | 107 | SortBench(void* param, Type t, int n, SortType s) : INHERITED(param) { |
| 108 | if (n > MAX) { |
| 109 | n = MAX; |
mike@reedtribe.org | a3f1c8e | 2013-01-23 02:23:40 +0000 | [diff] [blame] | 110 | } |
| 111 | fName.printf("sort_%s_%s", gSorts[s].fName, gRec[t].fName); |
reed@google.com | eebdd96 | 2013-01-23 14:52:12 +0000 | [diff] [blame^] | 112 | fCount = n; |
| 113 | gRec[t].fProc(fUnsorted, n); |
mike@reedtribe.org | a3f1c8e | 2013-01-23 02:23:40 +0000 | [diff] [blame] | 114 | fSortProc = gSorts[s].fProc; |
| 115 | fIsRendering = false; |
| 116 | } |
| 117 | |
| 118 | protected: |
| 119 | virtual const char* onGetName() SK_OVERRIDE { |
| 120 | return fName.c_str(); |
| 121 | } |
| 122 | |
| 123 | virtual void onDraw(SkCanvas* canvas) { |
reed@google.com | eebdd96 | 2013-01-23 14:52:12 +0000 | [diff] [blame^] | 124 | int n = SkBENCHLOOP(200); |
mike@reedtribe.org | a3f1c8e | 2013-01-23 02:23:40 +0000 | [diff] [blame] | 125 | for (int i = 0; i < n; i++) { |
| 126 | memcpy(fSorted, fUnsorted, fCount * sizeof(int)); |
| 127 | fSortProc(fSorted, fCount); |
| 128 | #ifdef SK_DEBUG |
| 129 | for (int j = 1; j < fCount; ++j) { |
| 130 | SkASSERT(fSorted[j - 1] <= fSorted[j]); |
| 131 | } |
| 132 | #endif |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | private: |
| 137 | typedef SkBenchmark INHERITED; |
| 138 | }; |
| 139 | |
| 140 | /////////////////////////////////////////////////////////////////////////////// |
| 141 | |
mike@reedtribe.org | a3f1c8e | 2013-01-23 02:23:40 +0000 | [diff] [blame] | 142 | static SkBenchmark* NewSkQSort(void* param, Type t) { |
| 143 | return new SortBench(param, t, N, kSKQSort); |
| 144 | } |
mike@reedtribe.org | a3f1c8e | 2013-01-23 02:23:40 +0000 | [diff] [blame] | 145 | static SkBenchmark* NewSkHeap(void* param, Type t) { |
| 146 | return new SortBench(param, t, N, kSKHeap); |
| 147 | } |
mike@reedtribe.org | a3f1c8e | 2013-01-23 02:23:40 +0000 | [diff] [blame] | 148 | static SkBenchmark* NewQSort(void* param, Type t) { |
| 149 | return new SortBench(param, t, N, kQSort); |
| 150 | } |
| 151 | |
reed@google.com | eebdd96 | 2013-01-23 14:52:12 +0000 | [diff] [blame^] | 152 | DEF_BENCH( return NewSkQSort(p, kRand); ) |
| 153 | DEF_BENCH( return NewSkHeap(p, kRand); ) |
mike@reedtribe.org | a3f1c8e | 2013-01-23 02:23:40 +0000 | [diff] [blame] | 154 | DEF_BENCH( return NewQSort(p, kRand); ) |
reed@google.com | eebdd96 | 2013-01-23 14:52:12 +0000 | [diff] [blame^] | 155 | |
| 156 | DEF_BENCH( return NewSkQSort(p, kRandN); ) |
| 157 | DEF_BENCH( return NewSkHeap(p, kRandN); ) |
| 158 | DEF_BENCH( return NewQSort(p, kRandN); ) |
| 159 | |
| 160 | DEF_BENCH( return NewSkQSort(p, kFore); ) |
| 161 | DEF_BENCH( return NewSkHeap(p, kFore); ) |
mike@reedtribe.org | a3f1c8e | 2013-01-23 02:23:40 +0000 | [diff] [blame] | 162 | DEF_BENCH( return NewQSort(p, kFore); ) |
reed@google.com | eebdd96 | 2013-01-23 14:52:12 +0000 | [diff] [blame^] | 163 | |
| 164 | DEF_BENCH( return NewSkQSort(p, kBack); ) |
| 165 | DEF_BENCH( return NewSkHeap(p, kBack); ) |
mike@reedtribe.org | a3f1c8e | 2013-01-23 02:23:40 +0000 | [diff] [blame] | 166 | DEF_BENCH( return NewQSort(p, kBack); ) |
reed@google.com | eebdd96 | 2013-01-23 14:52:12 +0000 | [diff] [blame^] | 167 | |
| 168 | DEF_BENCH( return NewSkQSort(p, kSame); ) |
| 169 | DEF_BENCH( return NewSkHeap(p, kSame); ) |
mike@reedtribe.org | a3f1c8e | 2013-01-23 02:23:40 +0000 | [diff] [blame] | 170 | DEF_BENCH( return NewQSort(p, kSame); ) |
| 171 | |