commit-bot@chromium.org | 4fcc3ca | 2014-02-27 20:23:22 +0000 | [diff] [blame] | 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 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 8 | #include "Benchmark.h" |
commit-bot@chromium.org | 4fcc3ca | 2014-02-27 20:23:22 +0000 | [diff] [blame] | 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 |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 18 | class GrOrderedSetBuildBench : public Benchmark { |
commit-bot@chromium.org | 4fcc3ca | 2014-02-27 20:23:22 +0000 | [diff] [blame] | 19 | public: |
| 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 | |
| 30 | protected: |
| 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 | |
| 52 | private: |
| 53 | SkString fName; |
| 54 | int fData[NUM_ELEMENTS]; |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 55 | typedef Benchmark INHERITED; |
commit-bot@chromium.org | 4fcc3ca | 2014-02-27 20:23:22 +0000 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | // Time how long it takes to find elements in a set |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 59 | class GrOrderedSetFindBench : public Benchmark { |
commit-bot@chromium.org | 4fcc3ca | 2014-02-27 20:23:22 +0000 | [diff] [blame] | 60 | public: |
| 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 | |
| 71 | protected: |
| 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 | |
| 92 | private: |
| 93 | SkString fName; |
| 94 | int fData[NUM_ELEMENTS]; |
| 95 | GrOrderedSet<int> fSet; |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 96 | typedef Benchmark INHERITED; |
commit-bot@chromium.org | 4fcc3ca | 2014-02-27 20:23:22 +0000 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | // Time how long it takes to iterate over and remove all elements from set |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 100 | class GrOrderedSetRemoveBench : public Benchmark { |
commit-bot@chromium.org | 4fcc3ca | 2014-02-27 20:23:22 +0000 | [diff] [blame] | 101 | public: |
| 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 | |
| 112 | protected: |
| 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 | |
| 137 | private: |
| 138 | SkString fName; |
| 139 | GrOrderedSet<int> fSet; |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 140 | typedef Benchmark INHERITED; |
commit-bot@chromium.org | 4fcc3ca | 2014-02-27 20:23:22 +0000 | [diff] [blame] | 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 |