blob: dde1c4a3f0d64745e97cef374e0821aa147c97c7 [file] [log] [blame]
commit-bot@chromium.org4fcc3ca2014-02-27 20:23:22 +00001/*
2 * Copyright 2014 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
tfarinaf168b862014-06-19 12:32:29 -07008#include "Benchmark.h"
commit-bot@chromium.org4fcc3ca2014-02-27 20:23:22 +00009#include "SkCanvas.h"
10#include "SkRandom.h"
11#include "SkString.h"
12#if SK_SUPPORT_GPU
13#include "GrOrderedSet.h"
14
15static const int NUM_ELEMENTS = 1000;
16
17// Time how long it takes to build a set
tfarinaf168b862014-06-19 12:32:29 -070018class GrOrderedSetBuildBench : public Benchmark {
commit-bot@chromium.org4fcc3ca2014-02-27 20:23:22 +000019public:
20 GrOrderedSetBuildBench() {
21 fName.append("ordered_set_build");
22 }
23
24 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
25 return kNonRendering_Backend == backend;
26 }
27
28 virtual ~GrOrderedSetBuildBench() {}
29
30protected:
31 virtual const char* onGetName() SK_OVERRIDE {
32 return fName.c_str();
33 }
34
35 virtual void onPreDraw() SK_OVERRIDE {
36 SkRandom rand;
37 for (int j = 0; j < NUM_ELEMENTS; ++j) {
38 fData[j] = rand.nextU() % NUM_ELEMENTS;
39 }
40 }
41
42 virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
43 for (int i = 0; i < loops; ++i) {
44 GrOrderedSet<int> set;
45 for (int j = 0; j < NUM_ELEMENTS; ++j) {
46 set.insert(fData[j]);
47 }
48 set.reset();
49 }
50 }
51
52private:
53 SkString fName;
54 int fData[NUM_ELEMENTS];
tfarinaf168b862014-06-19 12:32:29 -070055 typedef Benchmark INHERITED;
commit-bot@chromium.org4fcc3ca2014-02-27 20:23:22 +000056};
57
58// Time how long it takes to find elements in a set
tfarinaf168b862014-06-19 12:32:29 -070059class GrOrderedSetFindBench : public Benchmark {
commit-bot@chromium.org4fcc3ca2014-02-27 20:23:22 +000060public:
61 GrOrderedSetFindBench() {
62 fName.append("ordered_set_find");
63 }
64
65 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
66 return kNonRendering_Backend == backend;
67 }
68
69 virtual ~GrOrderedSetFindBench() {}
70
71protected:
72 virtual const char* onGetName() SK_OVERRIDE {
73 return fName.c_str();
74 }
75
76 virtual void onPreDraw() SK_OVERRIDE {
77 SkRandom rand;
78 for (int j = 0; j < NUM_ELEMENTS; ++j) {
79 fData[j] = rand.nextU() % 1500;
80 fSet.insert(rand.nextU() % NUM_ELEMENTS);
81 }
82 }
83
84 virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
85 for (int i = 0; i < loops; ++i) {
86 for (int j = 0; j < NUM_ELEMENTS; ++j) {
87 fSet.find(fData[j]);
88 }
89 }
90 }
91
92private:
93 SkString fName;
94 int fData[NUM_ELEMENTS];
95 GrOrderedSet<int> fSet;
tfarinaf168b862014-06-19 12:32:29 -070096 typedef Benchmark INHERITED;
commit-bot@chromium.org4fcc3ca2014-02-27 20:23:22 +000097};
98
99// Time how long it takes to iterate over and remove all elements from set
tfarinaf168b862014-06-19 12:32:29 -0700100class GrOrderedSetRemoveBench : public Benchmark {
commit-bot@chromium.org4fcc3ca2014-02-27 20:23:22 +0000101public:
102 GrOrderedSetRemoveBench() {
103 fName.append("ordered_set_remove");
104 }
105
106 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
107 return kNonRendering_Backend == backend;
108 }
109
110 virtual ~GrOrderedSetRemoveBench() {}
111
112protected:
113 virtual const char* onGetName() SK_OVERRIDE {
114 return fName.c_str();
115 }
116
117 virtual void onPreDraw() SK_OVERRIDE {
118 SkRandom rand;
119 for (int j = 0; j < NUM_ELEMENTS; ++j) {
120 fSet.insert(rand.nextU() % NUM_ELEMENTS);
121 }
122 }
123
124 virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
125 typedef GrOrderedSet<int>::Iter SetIter;
126 for (int i = 0; i < loops; ++i) {
127 GrOrderedSet<int> testSet;
128 for (SetIter s = fSet.begin(); fSet.end() != s; ++s) {
129 testSet.insert(*s);
130 }
131 for (int j = 0; j < NUM_ELEMENTS; ++j) {
132 testSet.remove(testSet.find(j));
133 }
134 }
135 }
136
137private:
138 SkString fName;
139 GrOrderedSet<int> fSet;
tfarinaf168b862014-06-19 12:32:29 -0700140 typedef Benchmark INHERITED;
commit-bot@chromium.org4fcc3ca2014-02-27 20:23:22 +0000141};
142
143///////////////////////////////////////////////////////////////////////////////
144
145DEF_BENCH(return SkNEW_ARGS(GrOrderedSetBuildBench, ());)
146DEF_BENCH(return SkNEW_ARGS(GrOrderedSetFindBench, ());)
147DEF_BENCH(return SkNEW_ARGS(GrOrderedSetRemoveBench, ());)
148#endif