2 * Copyright 2013 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
13 static const int N = 1000;
15 static void rand_proc(int array[N]) {
17 for (int i = 0; i < N; ++i) {
18 array[i] = rand.nextS();
22 static void randN_proc(int array[N]) {
25 for (int i = 0; i < N; ++i) {
26 array[i] = rand.nextU() % mod;
30 static void forward_proc(int array[N]) {
31 for (int i = 0; i < N; ++i) {
36 static void backward_proc(int array[N]) {
37 for (int i = 0; i < N; ++i) {
42 static void same_proc(int array[N]) {
43 for (int i = 0; i < N; ++i) {
48 typedef void (*SortProc)(int array[N]);
51 kRand, kRandN, kFore, kBack, kSame
58 { "rand", rand_proc },
59 { "rand10", randN_proc },
60 { "forward", forward_proc },
61 { "backward", backward_proc },
62 { "repeated", same_proc },
65 static void skqsort_sort(int array[N]) {
66 // End is inclusive for SkTQSort!
67 SkTQSort<int>(array, array + N - 1);
70 static void skheap_sort(int array[N]) {
71 SkTHeapSort<int>(array, N);
75 static int int_compare(const void* a, const void* b) {
76 const int ai = *(const int*)a;
77 const int bi = *(const int*)b;
78 return ai < bi ? -1 : (ai > bi);
82 static void qsort_sort(int array[N]) {
83 qsort(array, N, sizeof(int), int_compare);
87 kSKQSort, kSKHeap, kQSort
94 { "skqsort", skqsort_sort },
95 { "skheap", skheap_sort },
96 { "qsort", qsort_sort },
99 class SortBench : public Benchmark {
102 const SortProc fSortProc;
103 SkAutoTMalloc<int> fUnsorted;
106 SortBench(Type t, SortType s) : fType(t), fSortProc(gSorts[s].fProc) {
107 fName.printf("sort_%s_%s", gSorts[s].fName, gRec[t].fName);
110 bool isSuitableFor(Backend backend) override {
111 return backend == kNonRendering_Backend;
115 const char* onGetName() override {
116 return fName.c_str();
119 // Delayed initialization only done if onDraw will be called.
120 void onPreDraw() override {
122 gRec[fType].fProc(fUnsorted.get());
125 void onDraw(const int loops, SkCanvas*) override {
126 SkAutoTMalloc<int> sorted(N);
127 for (int i = 0; i < loops; i++) {
128 memcpy(sorted.get(), fUnsorted.get(), N*sizeof(int));
129 fSortProc(sorted.get());
131 for (int j = 1; j < N; ++j) {
132 SkASSERT(sorted[j - 1] <= sorted[j]);
139 typedef Benchmark INHERITED;
142 ///////////////////////////////////////////////////////////////////////////////
144 static Benchmark* NewSkQSort(Type t) {
145 return new SortBench(t, kSKQSort);
147 static Benchmark* NewSkHeap(Type t) {
148 return new SortBench(t, kSKHeap);
150 static Benchmark* NewQSort(Type t) {
151 return new SortBench(t, kQSort);
154 DEF_BENCH( return NewSkQSort(kRand); )
155 DEF_BENCH( return NewSkHeap(kRand); )
156 DEF_BENCH( return NewQSort(kRand); )
158 DEF_BENCH( return NewSkQSort(kRandN); )
159 DEF_BENCH( return NewSkHeap(kRandN); )
160 DEF_BENCH( return NewQSort(kRandN); )
162 DEF_BENCH( return NewSkQSort(kFore); )
163 DEF_BENCH( return NewSkHeap(kFore); )
164 DEF_BENCH( return NewQSort(kFore); )
166 DEF_BENCH( return NewSkQSort(kBack); )
167 DEF_BENCH( return NewSkHeap(kBack); )
168 DEF_BENCH( return NewQSort(kBack); )
170 DEF_BENCH( return NewSkQSort(kSame); )
171 DEF_BENCH( return NewSkHeap(kSame); )
172 DEF_BENCH( return NewQSort(kSame); )