2013-01-23 02:23:40 +00:00
|
|
|
/*
|
|
|
|
* 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"
|
|
|
|
|
2013-01-23 14:52:12 +00:00
|
|
|
static const int N = 1000;
|
|
|
|
|
2013-01-23 02:23:40 +00:00
|
|
|
static void rand_proc(int array[], int count) {
|
2013-09-09 20:09:12 +00:00
|
|
|
SkRandom rand;
|
2013-01-23 02:23:40 +00:00
|
|
|
for (int i = 0; i < count; ++i) {
|
|
|
|
array[i] = rand.nextS();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-23 14:52:12 +00:00
|
|
|
static void randN_proc(int array[], int count) {
|
2013-09-09 20:09:12 +00:00
|
|
|
SkRandom rand;
|
2013-01-23 14:52:12 +00:00
|
|
|
int mod = N / 10;
|
|
|
|
for (int i = 0; i < count; ++i) {
|
|
|
|
array[i] = rand.nextU() % mod;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-23 02:23:40 +00:00
|
|
|
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 {
|
2013-01-23 14:52:12 +00:00
|
|
|
kRand, kRandN, kFore, kBack, kSame
|
2013-01-23 02:23:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const struct {
|
|
|
|
const char* fName;
|
|
|
|
SortProc fProc;
|
|
|
|
} gRec[] = {
|
2013-01-23 14:52:12 +00:00
|
|
|
{ "rand", rand_proc },
|
|
|
|
{ "rand10", randN_proc },
|
2013-01-23 02:23:40 +00:00
|
|
|
{ "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:
|
2013-09-13 19:52:27 +00:00
|
|
|
SortBench(Type t, int n, SortType s) {
|
2013-01-23 14:52:12 +00:00
|
|
|
if (n > MAX) {
|
|
|
|
n = MAX;
|
2013-01-23 02:23:40 +00:00
|
|
|
}
|
|
|
|
fName.printf("sort_%s_%s", gSorts[s].fName, gRec[t].fName);
|
2013-01-23 14:52:12 +00:00
|
|
|
fCount = n;
|
|
|
|
gRec[t].fProc(fUnsorted, n);
|
2013-01-23 02:23:40 +00:00
|
|
|
fSortProc = gSorts[s].fProc;
|
|
|
|
fIsRendering = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual const char* onGetName() SK_OVERRIDE {
|
|
|
|
return fName.c_str();
|
|
|
|
}
|
|
|
|
|
2013-03-05 18:50:01 +00:00
|
|
|
virtual void onDraw(SkCanvas*) {
|
2013-09-10 19:23:38 +00:00
|
|
|
for (int i = 0; i < this->getLoops(); i++) {
|
2013-01-23 02:23:40 +00:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2013-09-13 19:52:27 +00:00
|
|
|
static SkBenchmark* NewSkQSort(Type t) {
|
|
|
|
return new SortBench(t, N, kSKQSort);
|
2013-01-23 02:23:40 +00:00
|
|
|
}
|
2013-09-13 19:52:27 +00:00
|
|
|
static SkBenchmark* NewSkHeap(Type t) {
|
|
|
|
return new SortBench(t, N, kSKHeap);
|
2013-01-23 02:23:40 +00:00
|
|
|
}
|
2013-09-13 19:52:27 +00:00
|
|
|
static SkBenchmark* NewQSort(Type t) {
|
|
|
|
return new SortBench(t, N, kQSort);
|
2013-01-23 02:23:40 +00:00
|
|
|
}
|
|
|
|
|
2013-09-13 19:52:27 +00:00
|
|
|
DEF_BENCH( return NewSkQSort(kRand); )
|
|
|
|
DEF_BENCH( return NewSkHeap(kRand); )
|
|
|
|
DEF_BENCH( return NewQSort(kRand); )
|
2013-01-23 14:52:12 +00:00
|
|
|
|
2013-09-13 19:52:27 +00:00
|
|
|
DEF_BENCH( return NewSkQSort(kRandN); )
|
|
|
|
DEF_BENCH( return NewSkHeap(kRandN); )
|
|
|
|
DEF_BENCH( return NewQSort(kRandN); )
|
2013-01-23 14:52:12 +00:00
|
|
|
|
2013-09-13 19:52:27 +00:00
|
|
|
DEF_BENCH( return NewSkQSort(kFore); )
|
|
|
|
DEF_BENCH( return NewSkHeap(kFore); )
|
|
|
|
DEF_BENCH( return NewQSort(kFore); )
|
2013-01-23 14:52:12 +00:00
|
|
|
|
2013-09-13 19:52:27 +00:00
|
|
|
DEF_BENCH( return NewSkQSort(kBack); )
|
|
|
|
DEF_BENCH( return NewSkHeap(kBack); )
|
|
|
|
DEF_BENCH( return NewQSort(kBack); )
|
2013-01-23 14:52:12 +00:00
|
|
|
|
2013-09-13 19:52:27 +00:00
|
|
|
DEF_BENCH( return NewSkQSort(kSame); )
|
|
|
|
DEF_BENCH( return NewSkHeap(kSame); )
|
|
|
|
DEF_BENCH( return NewQSort(kSame); )
|