1) Don't allocate 100,000 ints when we only need 1,000.

2) Don't sort past the end of the array when using SkTQSort.
3) Don't allocate any arrays until we need them (in onDraw).

Also, propagate the constant N everywhere through the bench.

BUG=
R=bungeman@google.com

Review URL: https://codereview.chromium.org/23890033

git-svn-id: http://skia.googlecode.com/svn/trunk@11329 2bbb7eff-a529-9590-31e7-b0007b416f81
This commit is contained in:
mtklein@google.com 2013-09-17 19:37:34 +00:00
Родитель 93d2f559b8
Коммит b352627436
1 изменённых файлов: 40 добавлений и 40 удалений

Просмотреть файл

@ -12,40 +12,40 @@
static const int N = 1000; static const int N = 1000;
static void rand_proc(int array[], int count) { static void rand_proc(int array[N]) {
SkRandom rand; SkRandom rand;
for (int i = 0; i < count; ++i) { for (int i = 0; i < N; ++i) {
array[i] = rand.nextS(); array[i] = rand.nextS();
} }
} }
static void randN_proc(int array[], int count) { static void randN_proc(int array[N]) {
SkRandom rand; SkRandom rand;
int mod = N / 10; int mod = N / 10;
for (int i = 0; i < count; ++i) { for (int i = 0; i < N; ++i) {
array[i] = rand.nextU() % mod; array[i] = rand.nextU() % mod;
} }
} }
static void forward_proc(int array[], int count) { static void forward_proc(int array[N]) {
for (int i = 0; i < count; ++i) { for (int i = 0; i < N; ++i) {
array[i] = i; array[i] = i;
} }
} }
static void backward_proc(int array[], int count) { static void backward_proc(int array[N]) {
for (int i = 0; i < count; ++i) { for (int i = 0; i < N; ++i) {
array[i] = -i; array[i] = -i;
} }
} }
static void same_proc(int array[], int count) { static void same_proc(int array[N]) {
for (int i = 0; i < count; ++i) { for (int i = 0; i < N; ++i) {
array[i] = count; array[i] = N;
} }
} }
typedef void (*SortProc)(int array[], int count); typedef void (*SortProc)(int array[N]);
enum Type { enum Type {
kRand, kRandN, kFore, kBack, kSame kRand, kRandN, kFore, kBack, kSame
@ -62,12 +62,13 @@ static const struct {
{ "repeated", same_proc }, { "repeated", same_proc },
}; };
static void skqsort_sort(int array[], int count) { static void skqsort_sort(int array[N]) {
SkTQSort<int>(array, array + count); // End is inclusive for SkTQSort!
SkTQSort<int>(array, array + N - 1);
} }
static void skheap_sort(int array[], int count) { static void skheap_sort(int array[N]) {
SkTHeapSort<int>(array, count); SkTHeapSort<int>(array, N);
} }
extern "C" { extern "C" {
@ -78,8 +79,8 @@ extern "C" {
} }
} }
static void qsort_sort(int array[], int count) { static void qsort_sort(int array[N]) {
qsort(array, count, sizeof(int), int_compare); qsort(array, N, sizeof(int), int_compare);
} }
enum SortType { enum SortType {
@ -96,23 +97,15 @@ static const struct {
}; };
class SortBench : public SkBenchmark { class SortBench : public SkBenchmark {
SkString fName; SkString fName;
enum { MAX = 100000 }; const Type fType;
int fUnsorted[MAX]; const SortProc fSortProc;
int fSorted[MAX]; SkAutoTMalloc<int> fUnsorted;
int fCount;
SortProc fSortProc;
public: public:
SortBench(Type t, int n, SortType s) { SortBench(Type t, SortType s) : fType(t), fSortProc(gSorts[s].fProc) {
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; fIsRendering = false;
fName.printf("sort_%s_%s", gSorts[s].fName, gRec[t].fName);
} }
protected: protected:
@ -120,13 +113,20 @@ protected:
return fName.c_str(); return fName.c_str();
} }
virtual void onDraw(SkCanvas*) { // Delayed initialization only done if onDraw will be called.
virtual void onPreDraw() SK_OVERRIDE {
fUnsorted.reset(N);
gRec[fType].fProc(fUnsorted.get());
}
virtual void onDraw(SkCanvas*) SK_OVERRIDE {
SkAutoTMalloc<int> sorted(N);
for (int i = 0; i < this->getLoops(); i++) { for (int i = 0; i < this->getLoops(); i++) {
memcpy(fSorted, fUnsorted, fCount * sizeof(int)); memcpy(sorted.get(), fUnsorted.get(), N*sizeof(int));
fSortProc(fSorted, fCount); fSortProc(sorted.get());
#ifdef SK_DEBUG #ifdef SK_DEBUG
for (int j = 1; j < fCount; ++j) { for (int j = 1; j < N; ++j) {
SkASSERT(fSorted[j - 1] <= fSorted[j]); SkASSERT(sorted[j - 1] <= sorted[j]);
} }
#endif #endif
} }
@ -139,13 +139,13 @@ private:
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
static SkBenchmark* NewSkQSort(Type t) { static SkBenchmark* NewSkQSort(Type t) {
return new SortBench(t, N, kSKQSort); return new SortBench(t, kSKQSort);
} }
static SkBenchmark* NewSkHeap(Type t) { static SkBenchmark* NewSkHeap(Type t) {
return new SortBench(t, N, kSKHeap); return new SortBench(t, kSKHeap);
} }
static SkBenchmark* NewQSort(Type t) { static SkBenchmark* NewQSort(Type t) {
return new SortBench(t, N, kQSort); return new SortBench(t, kQSort);
} }
DEF_BENCH( return NewSkQSort(kRand); ) DEF_BENCH( return NewSkQSort(kRand); )