Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(439)

Side by Side Diff: bench/SortBench.cpp

Issue 23890033: Some fixes for SortBench (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: bungeman Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2013 Google Inc. 2 * Copyright 2013 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "SkBenchmark.h" 8 #include "SkBenchmark.h"
9 #include "SkRandom.h" 9 #include "SkRandom.h"
10 #include "SkTSort.h" 10 #include "SkTSort.h"
11 #include "SkString.h" 11 #include "SkString.h"
12 12
13 static const int N = 1000; 13 static const int N = 1000;
14 14
15 static void rand_proc(int array[], int count) { 15 static void rand_proc(int array[N]) {
16 SkRandom rand; 16 SkRandom rand;
17 for (int i = 0; i < count; ++i) { 17 for (int i = 0; i < N; ++i) {
18 array[i] = rand.nextS(); 18 array[i] = rand.nextS();
19 } 19 }
20 } 20 }
21 21
22 static void randN_proc(int array[], int count) { 22 static void randN_proc(int array[N]) {
23 SkRandom rand; 23 SkRandom rand;
24 int mod = N / 10; 24 int mod = N / 10;
25 for (int i = 0; i < count; ++i) { 25 for (int i = 0; i < N; ++i) {
26 array[i] = rand.nextU() % mod; 26 array[i] = rand.nextU() % mod;
27 } 27 }
28 } 28 }
29 29
30 static void forward_proc(int array[], int count) { 30 static void forward_proc(int array[N]) {
31 for (int i = 0; i < count; ++i) { 31 for (int i = 0; i < N; ++i) {
32 array[i] = i; 32 array[i] = i;
33 } 33 }
34 } 34 }
35 35
36 static void backward_proc(int array[], int count) { 36 static void backward_proc(int array[N]) {
37 for (int i = 0; i < count; ++i) { 37 for (int i = 0; i < N; ++i) {
38 array[i] = -i; 38 array[i] = -i;
39 } 39 }
40 } 40 }
41 41
42 static void same_proc(int array[], int count) { 42 static void same_proc(int array[N]) {
43 for (int i = 0; i < count; ++i) { 43 for (int i = 0; i < N; ++i) {
44 array[i] = count; 44 array[i] = N;
45 } 45 }
46 } 46 }
47 47
48 typedef void (*SortProc)(int array[], int count); 48 typedef void (*SortProc)(int array[N]);
49 49
50 enum Type { 50 enum Type {
51 kRand, kRandN, kFore, kBack, kSame 51 kRand, kRandN, kFore, kBack, kSame
52 }; 52 };
53 53
54 static const struct { 54 static const struct {
55 const char* fName; 55 const char* fName;
56 SortProc fProc; 56 SortProc fProc;
57 } gRec[] = { 57 } gRec[] = {
58 { "rand", rand_proc }, 58 { "rand", rand_proc },
59 { "rand10", randN_proc }, 59 { "rand10", randN_proc },
60 { "forward", forward_proc }, 60 { "forward", forward_proc },
61 { "backward", backward_proc }, 61 { "backward", backward_proc },
62 { "repeated", same_proc }, 62 { "repeated", same_proc },
63 }; 63 };
64 64
65 static void skqsort_sort(int array[], int count) { 65 static void skqsort_sort(int array[N]) {
66 SkTQSort<int>(array, array + count); 66 // End is inclusive for SkTQSort!
67 SkTQSort<int>(array, array + N - 1);
67 } 68 }
68 69
69 static void skheap_sort(int array[], int count) { 70 static void skheap_sort(int array[N]) {
70 SkTHeapSort<int>(array, count); 71 SkTHeapSort<int>(array, N);
71 } 72 }
72 73
73 extern "C" { 74 extern "C" {
74 static int int_compare(const void* a, const void* b) { 75 static int int_compare(const void* a, const void* b) {
75 const int ai = *(const int*)a; 76 const int ai = *(const int*)a;
76 const int bi = *(const int*)b; 77 const int bi = *(const int*)b;
77 return ai < bi ? -1 : (ai > bi); 78 return ai < bi ? -1 : (ai > bi);
78 } 79 }
79 } 80 }
80 81
81 static void qsort_sort(int array[], int count) { 82 static void qsort_sort(int array[N]) {
82 qsort(array, count, sizeof(int), int_compare); 83 qsort(array, N, sizeof(int), int_compare);
83 } 84 }
84 85
85 enum SortType { 86 enum SortType {
86 kSKQSort, kSKHeap, kQSort 87 kSKQSort, kSKHeap, kQSort
87 }; 88 };
88 89
89 static const struct { 90 static const struct {
90 const char* fName; 91 const char* fName;
91 SortProc fProc; 92 SortProc fProc;
92 } gSorts[] = { 93 } gSorts[] = {
93 { "skqsort", skqsort_sort }, 94 { "skqsort", skqsort_sort },
94 { "skheap", skheap_sort }, 95 { "skheap", skheap_sort },
95 { "qsort", qsort_sort }, 96 { "qsort", qsort_sort },
96 }; 97 };
97 98
98 class SortBench : public SkBenchmark { 99 class SortBench : public SkBenchmark {
99 SkString fName; 100 SkString fName;
100 enum { MAX = 100000 }; 101 const Type fType;
101 int fUnsorted[MAX]; 102 const SortProc fSortProc;
102 int fSorted[MAX]; 103 SkAutoTMalloc<int> fUnsorted;
103 int fCount;
104 SortProc fSortProc;
105 104
106 public: 105 public:
107 SortBench(Type t, int n, SortType s) { 106 SortBench(Type t, SortType s) : fType(t), fSortProc(gSorts[s].fProc) {
108 if (n > MAX) { 107 fIsRendering = false;
109 n = MAX;
110 }
111 fName.printf("sort_%s_%s", gSorts[s].fName, gRec[t].fName); 108 fName.printf("sort_%s_%s", gSorts[s].fName, gRec[t].fName);
112 fCount = n;
113 gRec[t].fProc(fUnsorted, n);
114 fSortProc = gSorts[s].fProc;
115 fIsRendering = false;
116 } 109 }
117 110
118 protected: 111 protected:
119 virtual const char* onGetName() SK_OVERRIDE { 112 virtual const char* onGetName() SK_OVERRIDE {
120 return fName.c_str(); 113 return fName.c_str();
121 } 114 }
122 115
123 virtual void onDraw(SkCanvas*) { 116 // Delayed initialization only done if onDraw will be called.
117 virtual void onPreDraw() SK_OVERRIDE {
118 fUnsorted.reset(N);
119 gRec[fType].fProc(fUnsorted.get());
120 }
121
122 virtual void onDraw(SkCanvas*) SK_OVERRIDE {
123 SkAutoTMalloc<int> sorted(N);
124 for (int i = 0; i < this->getLoops(); i++) { 124 for (int i = 0; i < this->getLoops(); i++) {
125 memcpy(fSorted, fUnsorted, fCount * sizeof(int)); 125 memcpy(sorted.get(), fUnsorted.get(), N*sizeof(int));
126 fSortProc(fSorted, fCount); 126 fSortProc(sorted.get());
127 #ifdef SK_DEBUG 127 #ifdef SK_DEBUG
128 for (int j = 1; j < fCount; ++j) { 128 for (int j = 1; j < N; ++j) {
129 SkASSERT(fSorted[j - 1] <= fSorted[j]); 129 SkASSERT(sorted[j - 1] <= sorted[j]);
130 } 130 }
131 #endif 131 #endif
132 } 132 }
133 } 133 }
134 134
135 private: 135 private:
136 typedef SkBenchmark INHERITED; 136 typedef SkBenchmark INHERITED;
137 }; 137 };
138 138
139 /////////////////////////////////////////////////////////////////////////////// 139 ///////////////////////////////////////////////////////////////////////////////
140 140
141 static SkBenchmark* NewSkQSort(Type t) { 141 static SkBenchmark* NewSkQSort(Type t) {
142 return new SortBench(t, N, kSKQSort); 142 return new SortBench(t, kSKQSort);
143 } 143 }
144 static SkBenchmark* NewSkHeap(Type t) { 144 static SkBenchmark* NewSkHeap(Type t) {
145 return new SortBench(t, N, kSKHeap); 145 return new SortBench(t, kSKHeap);
146 } 146 }
147 static SkBenchmark* NewQSort(Type t) { 147 static SkBenchmark* NewQSort(Type t) {
148 return new SortBench(t, N, kQSort); 148 return new SortBench(t, kQSort);
149 } 149 }
150 150
151 DEF_BENCH( return NewSkQSort(kRand); ) 151 DEF_BENCH( return NewSkQSort(kRand); )
152 DEF_BENCH( return NewSkHeap(kRand); ) 152 DEF_BENCH( return NewSkHeap(kRand); )
153 DEF_BENCH( return NewQSort(kRand); ) 153 DEF_BENCH( return NewQSort(kRand); )
154 154
155 DEF_BENCH( return NewSkQSort(kRandN); ) 155 DEF_BENCH( return NewSkQSort(kRandN); )
156 DEF_BENCH( return NewSkHeap(kRandN); ) 156 DEF_BENCH( return NewSkHeap(kRandN); )
157 DEF_BENCH( return NewQSort(kRandN); ) 157 DEF_BENCH( return NewQSort(kRandN); )
158 158
159 DEF_BENCH( return NewSkQSort(kFore); ) 159 DEF_BENCH( return NewSkQSort(kFore); )
160 DEF_BENCH( return NewSkHeap(kFore); ) 160 DEF_BENCH( return NewSkHeap(kFore); )
161 DEF_BENCH( return NewQSort(kFore); ) 161 DEF_BENCH( return NewQSort(kFore); )
162 162
163 DEF_BENCH( return NewSkQSort(kBack); ) 163 DEF_BENCH( return NewSkQSort(kBack); )
164 DEF_BENCH( return NewSkHeap(kBack); ) 164 DEF_BENCH( return NewSkHeap(kBack); )
165 DEF_BENCH( return NewQSort(kBack); ) 165 DEF_BENCH( return NewQSort(kBack); )
166 166
167 DEF_BENCH( return NewSkQSort(kSame); ) 167 DEF_BENCH( return NewSkQSort(kSame); )
168 DEF_BENCH( return NewSkHeap(kSame); ) 168 DEF_BENCH( return NewSkHeap(kSame); )
169 DEF_BENCH( return NewQSort(kSame); ) 169 DEF_BENCH( return NewQSort(kSame); )
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698