1 /* 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 8 #include "Benchmark.h" 9 #include "SkCanvas.h" 10 #include "SkRandom.h" 11 #include "SkString.h" 12 #if SK_SUPPORT_GPU 13 #include "GrOrderedSet.h" 14 15 static const int NUM_ELEMENTS = 1000; 16 17 // Time how long it takes to build a set 18 class GrOrderedSetBuildBench : public Benchmark { 19 public: GrOrderedSetBuildBench()20 GrOrderedSetBuildBench() { 21 fName.append("ordered_set_build"); 22 } 23 isSuitableFor(Backend backend)24 bool isSuitableFor(Backend backend) override { 25 return kNonRendering_Backend == backend; 26 } 27 ~GrOrderedSetBuildBench()28 virtual ~GrOrderedSetBuildBench() {} 29 30 protected: onGetName()31 const char* onGetName() override { 32 return fName.c_str(); 33 } 34 onPreDraw()35 void onPreDraw() override { 36 SkRandom rand; 37 for (int j = 0; j < NUM_ELEMENTS; ++j) { 38 fData[j] = rand.nextU() % NUM_ELEMENTS; 39 } 40 } 41 onDraw(const int loops,SkCanvas * canvas)42 void onDraw(const int loops, SkCanvas* canvas) 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 52 private: 53 SkString fName; 54 int fData[NUM_ELEMENTS]; 55 typedef Benchmark INHERITED; 56 }; 57 58 // Time how long it takes to find elements in a set 59 class GrOrderedSetFindBench : public Benchmark { 60 public: GrOrderedSetFindBench()61 GrOrderedSetFindBench() { 62 fName.append("ordered_set_find"); 63 } 64 isSuitableFor(Backend backend)65 bool isSuitableFor(Backend backend) override { 66 return kNonRendering_Backend == backend; 67 } 68 ~GrOrderedSetFindBench()69 virtual ~GrOrderedSetFindBench() {} 70 71 protected: onGetName()72 const char* onGetName() override { 73 return fName.c_str(); 74 } 75 onPreDraw()76 void onPreDraw() 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 onDraw(const int loops,SkCanvas * canvas)84 void onDraw(const int loops, SkCanvas* canvas) 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 92 private: 93 SkString fName; 94 int fData[NUM_ELEMENTS]; 95 GrOrderedSet<int> fSet; 96 typedef Benchmark INHERITED; 97 }; 98 99 // Time how long it takes to iterate over and remove all elements from set 100 class GrOrderedSetRemoveBench : public Benchmark { 101 public: GrOrderedSetRemoveBench()102 GrOrderedSetRemoveBench() { 103 fName.append("ordered_set_remove"); 104 } 105 isSuitableFor(Backend backend)106 bool isSuitableFor(Backend backend) override { 107 return kNonRendering_Backend == backend; 108 } 109 ~GrOrderedSetRemoveBench()110 virtual ~GrOrderedSetRemoveBench() {} 111 112 protected: onGetName()113 const char* onGetName() override { 114 return fName.c_str(); 115 } 116 onPreDraw()117 void onPreDraw() override { 118 SkRandom rand; 119 for (int j = 0; j < NUM_ELEMENTS; ++j) { 120 fSet.insert(rand.nextU() % NUM_ELEMENTS); 121 } 122 } 123 onDraw(const int loops,SkCanvas * canvas)124 void onDraw(const int loops, SkCanvas* canvas) 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 137 private: 138 SkString fName; 139 GrOrderedSet<int> fSet; 140 typedef Benchmark INHERITED; 141 }; 142 143 /////////////////////////////////////////////////////////////////////////////// 144 145 DEF_BENCH(return SkNEW_ARGS(GrOrderedSetBuildBench, ());) 146 DEF_BENCH(return SkNEW_ARGS(GrOrderedSetFindBench, ());) 147 DEF_BENCH(return SkNEW_ARGS(GrOrderedSetRemoveBench, ());) 148 #endif 149