ChecksumBench.cpp revision 88db9ef0cd8646171f35a3325706291070b5dc55
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 "SkCanvas.h" 9#include "SkChecksum.h" 10#include "SkString.h" 11 12class ComputeChecksumBench : public SkBenchmark { 13public: 14 ComputeChecksumBench(void* param, const char name[]) : INHERITED(param) { 15 fName.printf("compute_checksum_%s", name); 16 } 17 18 enum { 19 DATA_SIZE = 1024, 20 N = SkBENCHLOOP(100000), 21 }; 22protected: 23 virtual const char* onGetName() { 24 return fName.c_str(); 25 } 26 27 virtual void onDraw(SkCanvas* canvas) { 28 uint64_t data[DATA_SIZE / sizeof(uint64_t)]; 29 computeChecksum(data, DATA_SIZE); 30 } 31 32 virtual void computeChecksum(const uint64_t*, size_t) = 0; 33 34 SkString fName; 35private: 36 typedef SkBenchmark INHERITED; 37}; 38 39/* 40 * Use SkComputeChecksum32 to compute a checksum on a datablock 41 */ 42class ComputeChecksum32Bench : public ComputeChecksumBench { 43public: 44 ComputeChecksum32Bench(void* param) 45 : INHERITED(param, "32") { } 46 47protected: 48 virtual void computeChecksum(const uint64_t* data, size_t len) { 49 for (int i = 0; i < N; i++) { 50 volatile uint32_t result = SkComputeChecksum32(reinterpret_cast<const uint32_t*>(data), len); 51 } 52 } 53 54private: 55 typedef ComputeChecksumBench INHERITED; 56}; 57 58/* 59 * Use SkComputeChecksum64 to compute a checksum on a datablock 60 */ 61class ComputeChecksum64Bench : public ComputeChecksumBench { 62public: 63 ComputeChecksum64Bench(void* param) 64 : INHERITED(param, "64") { } 65 66protected: 67 virtual void computeChecksum(const uint64_t* data, size_t len) { 68 for (int i = 0; i < N; i++) { 69 volatile uint64_t result = SkComputeChecksum64(data, len); 70 } 71 } 72 73private: 74 typedef ComputeChecksumBench INHERITED; 75}; 76 77/* 78 * Use SkComputeChecksum64 to compute a checksum on a datablock 79 */ 80class ComputeChecksumXXBench : public ComputeChecksumBench { 81public: 82 ComputeChecksumXXBench(void* param) : INHERITED(param, "XX") { } 83 84protected: 85 virtual void computeChecksum(const uint64_t* data, size_t len) { 86 for (int i = 0; i < N; i++) { 87 volatile uint32_t result = SkChecksum::Compute(reinterpret_cast<const uint32_t*>(data), len); 88 } 89 } 90 91private: 92 typedef ComputeChecksumBench INHERITED; 93}; 94 95/////////////////////////////////////////////////////////////////////////////// 96 97static SkBenchmark* Fact0(void* p) { return new ComputeChecksum32Bench(p); } 98static SkBenchmark* Fact1(void* p) { return new ComputeChecksum64Bench(p); } 99static SkBenchmark* Fact2(void* p) { return new ComputeChecksumXXBench(p); } 100 101static BenchRegistry gReg0(Fact0); 102static BenchRegistry gReg1(Fact1); 103static BenchRegistry gReg2(Fact2); 104