bungeman@google.com | 5548752 | 2012-05-14 14:09:24 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2012 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 | #include "SkBenchmark.h" |
| 8 | #include "SkThread.h" |
| 9 | #include <memory> |
| 10 | |
| 11 | enum { |
| 12 | N = SkBENCHLOOP(1000000), |
| 13 | M = SkBENCHLOOP(2) |
| 14 | }; |
| 15 | |
| 16 | class RefCntBench_Stack : public SkBenchmark { |
| 17 | public: |
| 18 | RefCntBench_Stack(void* param) : INHERITED(param) { |
| 19 | } |
| 20 | protected: |
| 21 | virtual const char* onGetName() { |
| 22 | return "ref_cnt_stack"; |
| 23 | } |
| 24 | |
| 25 | virtual void onDraw(SkCanvas* canvas) { |
| 26 | for (int i = 0; i < N; ++i) { |
| 27 | SkRefCnt ref; |
| 28 | for (int j = 0; j < M; ++j) { |
| 29 | ref.ref(); |
| 30 | ref.unref(); |
| 31 | } |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | private: |
| 36 | typedef SkBenchmark INHERITED; |
| 37 | }; |
| 38 | |
| 39 | class PlacedRefCnt : public SkRefCnt { |
| 40 | public: |
| 41 | PlacedRefCnt() : SkRefCnt() { } |
| 42 | void operator delete(void *p) { } |
| 43 | }; |
| 44 | |
| 45 | class RefCntBench_Heap : public SkBenchmark { |
| 46 | public: |
| 47 | RefCntBench_Heap(void* param) : INHERITED(param) { |
| 48 | } |
| 49 | protected: |
| 50 | virtual const char* onGetName() { |
| 51 | return "ref_cnt_heap"; |
| 52 | } |
| 53 | |
| 54 | virtual void onDraw(SkCanvas* canvas) { |
| 55 | char memory[sizeof(PlacedRefCnt)]; |
| 56 | for (int i = 0; i < N; ++i) { |
| 57 | PlacedRefCnt* ref = new (memory) PlacedRefCnt(); |
| 58 | for (int j = 0; j < M; ++j) { |
| 59 | ref->ref(); |
| 60 | ref->unref(); |
| 61 | } |
| 62 | ref->unref(); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | private: |
| 67 | typedef SkBenchmark INHERITED; |
| 68 | }; |
| 69 | |
| 70 | /////////////////////////////////////////////////////////////////////////////// |
| 71 | |
| 72 | static SkBenchmark* Fact0(void* p) { return new RefCntBench_Stack(p); } |
| 73 | static SkBenchmark* Fact1(void* p) { return new RefCntBench_Heap(p); } |
| 74 | |
| 75 | static BenchRegistry gReg01(Fact0); |
| 76 | static BenchRegistry gReg02(Fact1); |
| 77 | |