aboutsummaryrefslogtreecommitdiffhomepage
path: root/bench/SortBench.cpp
blob: c19ebae0c8ff21554ec22ceb44b46fd099c84292 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
 * Copyright 2013 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "SkBenchmark.h"
#include "SkRandom.h"
#include "SkTSort.h"
#include "SkString.h"

static const int N = 1000;

static void rand_proc(int array[], int count) {
    SkRandom rand;
    for (int i = 0; i < count; ++i) {
        array[i] = rand.nextS();
    }
}

static void randN_proc(int array[], int count) {
    SkRandom rand;
    int mod = N / 10;
    for (int i = 0; i < count; ++i) {
        array[i] = rand.nextU() % mod;
    }
}

static void forward_proc(int array[], int count) {
    for (int i = 0; i < count; ++i) {
        array[i] = i;
    }
}

static void backward_proc(int array[], int count) {
    for (int i = 0; i < count; ++i) {
        array[i] = -i;
    }
}

static void same_proc(int array[], int count) {
    for (int i = 0; i < count; ++i) {
        array[i] = count;
    }
}

typedef void (*SortProc)(int array[], int count);

enum Type {
    kRand, kRandN, kFore, kBack, kSame
};

static const struct {
    const char* fName;
    SortProc    fProc;
} gRec[] = {
    { "rand", rand_proc },
    { "rand10", randN_proc },
    { "forward", forward_proc },
    { "backward", backward_proc },
    { "repeated", same_proc },
};

static void skqsort_sort(int array[], int count) {
    SkTQSort<int>(array, array + count);
}

static void skheap_sort(int array[], int count) {
    SkTHeapSort<int>(array, count);
}

extern "C" {
    static int int_compare(const void* a, const void* b) {
        const int ai = *(const int*)a;
        const int bi = *(const int*)b;
        return ai < bi ? -1 : (ai > bi);
    }
}

static void qsort_sort(int array[], int count) {
    qsort(array, count, sizeof(int), int_compare);
}

enum SortType {
    kSKQSort, kSKHeap, kQSort
};

static const struct {
    const char* fName;
    SortProc    fProc;
} gSorts[] = {
    { "skqsort", skqsort_sort },
    { "skheap", skheap_sort },
    { "qsort", qsort_sort },
};

class SortBench : public SkBenchmark {
    SkString    fName;
    enum { MAX = 100000 };
    int         fUnsorted[MAX];
    int         fSorted[MAX];
    int         fCount;
    SortProc    fSortProc;

public:
    SortBench(void* param, Type t, int n, SortType s) : INHERITED(param) {
        if (n > MAX) {
            n = MAX;
        }
        fName.printf("sort_%s_%s", gSorts[s].fName, gRec[t].fName);
        fCount = n;
        gRec[t].fProc(fUnsorted, n);
        fSortProc = gSorts[s].fProc;
        fIsRendering = false;
    }

protected:
    virtual const char* onGetName() SK_OVERRIDE {
        return fName.c_str();
    }

    virtual void onDraw(SkCanvas*) {
        int n = SkBENCHLOOP(200);
        for (int i = 0; i < n; i++) {
            memcpy(fSorted, fUnsorted, fCount * sizeof(int));
            fSortProc(fSorted, fCount);
#ifdef SK_DEBUG
            for (int j = 1; j < fCount; ++j) {
                SkASSERT(fSorted[j - 1] <= fSorted[j]);
            }
#endif
        }
    }

private:
    typedef SkBenchmark INHERITED;
};

///////////////////////////////////////////////////////////////////////////////

static SkBenchmark* NewSkQSort(void* param, Type t) {
    return new SortBench(param, t, N, kSKQSort);
}
static SkBenchmark* NewSkHeap(void* param, Type t) {
    return new SortBench(param, t, N, kSKHeap);
}
static SkBenchmark* NewQSort(void* param, Type t) {
    return new SortBench(param, t, N, kQSort);
}

DEF_BENCH( return NewSkQSort(p, kRand); )
DEF_BENCH( return NewSkHeap(p, kRand); )
DEF_BENCH( return NewQSort(p, kRand); )

DEF_BENCH( return NewSkQSort(p, kRandN); )
DEF_BENCH( return NewSkHeap(p, kRandN); )
DEF_BENCH( return NewQSort(p, kRandN); )

DEF_BENCH( return NewSkQSort(p, kFore); )
DEF_BENCH( return NewSkHeap(p, kFore); )
DEF_BENCH( return NewQSort(p, kFore); )

DEF_BENCH( return NewSkQSort(p, kBack); )
DEF_BENCH( return NewSkHeap(p, kBack); )
DEF_BENCH( return NewQSort(p, kBack); )

DEF_BENCH( return NewSkQSort(p, kSame); )
DEF_BENCH( return NewSkHeap(p, kSame); )
DEF_BENCH( return NewQSort(p, kSame); )