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.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "bench/Benchmark.h"
|
|
|
|
#include "include/core/SkString.h"
|
|
|
|
#include "include/utils/SkRandom.h"
|
|
|
|
#include "src/core/SkTSort.h"
|
2018-09-19 15:31:27 +00:00
|
|
|
|
2018-05-31 18:37:39 +00:00
|
|
|
#include <algorithm>
|
2015-08-26 12:15:46 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2013-01-23 14:52:12 +00:00
|
|
|
static const int N = 1000;
|
|
|
|
|
2013-09-17 19:37:34 +00:00
|
|
|
static void rand_proc(int array[N]) {
|
2013-09-09 20:09:12 +00:00
|
|
|
SkRandom rand;
|
2013-09-17 19:37:34 +00:00
|
|
|
for (int i = 0; i < N; ++i) {
|
2013-01-23 02:23:40 +00:00
|
|
|
array[i] = rand.nextS();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-17 19:37:34 +00:00
|
|
|
static void randN_proc(int array[N]) {
|
2013-09-09 20:09:12 +00:00
|
|
|
SkRandom rand;
|
2013-01-23 14:52:12 +00:00
|
|
|
int mod = N / 10;
|
2013-09-17 19:37:34 +00:00
|
|
|
for (int i = 0; i < N; ++i) {
|
2013-01-23 14:52:12 +00:00
|
|
|
array[i] = rand.nextU() % mod;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-17 19:37:34 +00:00
|
|
|
static void forward_proc(int array[N]) {
|
|
|
|
for (int i = 0; i < N; ++i) {
|
2013-01-23 02:23:40 +00:00
|
|
|
array[i] = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-17 19:37:34 +00:00
|
|
|
static void backward_proc(int array[N]) {
|
|
|
|
for (int i = 0; i < N; ++i) {
|
2013-01-23 02:23:40 +00:00
|
|
|
array[i] = -i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-17 19:37:34 +00:00
|
|
|
static void same_proc(int array[N]) {
|
|
|
|
for (int i = 0; i < N; ++i) {
|
|
|
|
array[i] = N;
|
2013-01-23 02:23:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-17 19:37:34 +00:00
|
|
|
typedef void (*SortProc)(int array[N]);
|
2013-01-23 02:23:40 +00:00
|
|
|
|
|
|
|
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 },
|
|
|
|
};
|
|
|
|
|
2013-09-17 19:37:34 +00:00
|
|
|
static void skqsort_sort(int array[N]) {
|
|
|
|
// End is inclusive for SkTQSort!
|
|
|
|
SkTQSort<int>(array, array + N - 1);
|
2013-01-23 02:23:40 +00:00
|
|
|
}
|
|
|
|
|
2013-09-17 19:37:34 +00:00
|
|
|
static void skheap_sort(int array[N]) {
|
|
|
|
SkTHeapSort<int>(array, N);
|
2013-01-23 02:23:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-17 19:37:34 +00:00
|
|
|
static void qsort_sort(int array[N]) {
|
|
|
|
qsort(array, N, sizeof(int), int_compare);
|
2013-01-23 02:23:40 +00:00
|
|
|
}
|
|
|
|
|
2018-05-31 18:37:39 +00:00
|
|
|
static void stdsort_sort(int array[N]) {
|
|
|
|
std::sort(array, array+N);
|
|
|
|
}
|
|
|
|
|
2013-01-23 02:23:40 +00:00
|
|
|
enum SortType {
|
2018-05-31 18:37:39 +00:00
|
|
|
kSKQSort, kSKHeap, kQSort, kStdSort,
|
2013-01-23 02:23:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const struct {
|
|
|
|
const char* fName;
|
|
|
|
SortProc fProc;
|
|
|
|
} gSorts[] = {
|
|
|
|
{ "skqsort", skqsort_sort },
|
2018-05-31 18:37:39 +00:00
|
|
|
{ "skheap", skheap_sort },
|
|
|
|
{ "qsort", qsort_sort },
|
|
|
|
{ "stdsort", stdsort_sort },
|
2013-01-23 02:23:40 +00:00
|
|
|
};
|
|
|
|
|
2014-06-19 19:32:29 +00:00
|
|
|
class SortBench : public Benchmark {
|
2013-09-17 19:37:34 +00:00
|
|
|
SkString fName;
|
|
|
|
const Type fType;
|
|
|
|
const SortProc fSortProc;
|
|
|
|
SkAutoTMalloc<int> fUnsorted;
|
2013-01-23 02:23:40 +00:00
|
|
|
|
|
|
|
public:
|
2013-09-17 19:37:34 +00:00
|
|
|
SortBench(Type t, SortType s) : fType(t), fSortProc(gSorts[s].fProc) {
|
|
|
|
fName.printf("sort_%s_%s", gSorts[s].fName, gRec[t].fName);
|
2013-01-23 02:23:40 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
bool isSuitableFor(Backend backend) override {
|
2013-11-21 06:21:58 +00:00
|
|
|
return backend == kNonRendering_Backend;
|
|
|
|
}
|
|
|
|
|
2013-01-23 02:23:40 +00:00
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
const char* onGetName() override {
|
2013-01-23 02:23:40 +00:00
|
|
|
return fName.c_str();
|
|
|
|
}
|
|
|
|
|
2013-09-17 19:37:34 +00:00
|
|
|
// Delayed initialization only done if onDraw will be called.
|
2015-09-30 19:11:07 +00:00
|
|
|
void onDelayedSetup() override {
|
2013-09-17 19:37:34 +00:00
|
|
|
fUnsorted.reset(N);
|
|
|
|
gRec[fType].fProc(fUnsorted.get());
|
|
|
|
}
|
|
|
|
|
2015-10-01 16:43:39 +00:00
|
|
|
void onDraw(int loops, SkCanvas*) override {
|
2013-09-17 19:37:34 +00:00
|
|
|
SkAutoTMalloc<int> sorted(N);
|
2013-12-03 18:17:16 +00:00
|
|
|
for (int i = 0; i < loops; i++) {
|
2013-09-17 19:37:34 +00:00
|
|
|
memcpy(sorted.get(), fUnsorted.get(), N*sizeof(int));
|
|
|
|
fSortProc(sorted.get());
|
2013-01-23 02:23:40 +00:00
|
|
|
#ifdef SK_DEBUG
|
2013-09-17 19:37:34 +00:00
|
|
|
for (int j = 1; j < N; ++j) {
|
|
|
|
SkASSERT(sorted[j - 1] <= sorted[j]);
|
2013-01-23 02:23:40 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-06-19 19:32:29 +00:00
|
|
|
typedef Benchmark INHERITED;
|
2013-01-23 02:23:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2014-06-19 19:32:29 +00:00
|
|
|
static Benchmark* NewSkQSort(Type t) {
|
2013-09-17 19:37:34 +00:00
|
|
|
return new SortBench(t, kSKQSort);
|
2013-01-23 02:23:40 +00:00
|
|
|
}
|
2014-06-19 19:32:29 +00:00
|
|
|
static Benchmark* NewSkHeap(Type t) {
|
2013-09-17 19:37:34 +00:00
|
|
|
return new SortBench(t, kSKHeap);
|
2013-01-23 02:23:40 +00:00
|
|
|
}
|
2014-06-19 19:32:29 +00:00
|
|
|
static Benchmark* NewQSort(Type t) {
|
2013-09-17 19:37:34 +00:00
|
|
|
return new SortBench(t, kQSort);
|
2013-01-23 02:23:40 +00:00
|
|
|
}
|
2018-05-31 18:37:39 +00:00
|
|
|
static Benchmark* NewStdSort(Type t) {
|
|
|
|
return new SortBench(t, kStdSort);
|
|
|
|
}
|
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); )
|
2018-05-31 18:37:39 +00:00
|
|
|
DEF_BENCH( return NewStdSort(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); )
|
2018-05-31 18:37:39 +00:00
|
|
|
DEF_BENCH( return NewStdSort(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); )
|
2018-05-31 18:37:39 +00:00
|
|
|
DEF_BENCH( return NewStdSort(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); )
|
2018-05-31 18:37:39 +00:00
|
|
|
DEF_BENCH( return NewStdSort(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); )
|
2018-05-31 18:37:39 +00:00
|
|
|
DEF_BENCH( return NewStdSort(kSame); )
|