"...git@repo.buzztime.com:halo/packages_apps_launcher2.git" did not exist on "a13a2f2a7bd0550d1ad973f760ff25e1a4137c43"
ChecksumBench.cpp 2.88 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*
 * Copyright 2012 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */
#include "SkBenchmark.h"
#include "SkCanvas.h"
#include "SkChecksum.h"
10
#include "SkMD5.h"
11
#include "SkRandom.h"
12
#include "SkSHA1.h"
13
#include "SkTemplates.h"
14 15 16 17

enum ChecksumType {
    kChecksum_ChecksumType,
    kMD5_ChecksumType,
18 19
    kSHA1_ChecksumType,
    kMurmur3_ChecksumType,
20
};
21 22 23 24 25 26 27

class ComputeChecksumBench : public SkBenchmark {
    enum {
        U32COUNT  = 256,
        SIZE      = U32COUNT * 4,
    };
    uint32_t    fData[U32COUNT];
28
    ChecksumType fType;
29 30

public:
31
    ComputeChecksumBench(ChecksumType type) : fType(type) {
32 33 34 35 36 37 38 39 40
        SkRandom rand;
        for (int i = 0; i < U32COUNT; ++i) {
            fData[i] = rand.nextU();
        }
        fIsRendering = false;
    }

protected:
    virtual const char* onGetName() {
41 42 43 44
        switch (fType) {
            case kChecksum_ChecksumType: return "compute_checksum";
            case kMD5_ChecksumType: return "compute_md5";
            case kSHA1_ChecksumType: return "compute_sha1";
45 46
            case kMurmur3_ChecksumType: return "compute_murmur3";

47 48
            default: SK_CRASH(); return "";
        }
49 50
    }

51
    virtual void onDraw(SkCanvas*) {
52 53
        switch (fType) {
            case kChecksum_ChecksumType: {
54
                for (int i = 0; i < this->getLoops(); i++) {
55
                    volatile uint32_t result = SkChecksum::Compute(fData, sizeof(fData));
56
                    sk_ignore_unused_variable(result);
57 58 59
                }
            } break;
            case kMD5_ChecksumType: {
60
                for (int i = 0; i < this->getLoops(); i++) {
61 62 63 64 65 66 67
                    SkMD5 md5;
                    md5.update(reinterpret_cast<uint8_t*>(fData), sizeof(fData));
                    SkMD5::Digest digest;
                    md5.finish(digest);
                }
            } break;
            case kSHA1_ChecksumType: {
68
                for (int i = 0; i < this->getLoops(); i++) {
69 70 71 72 73 74
                    SkSHA1 sha1;
                    sha1.update(reinterpret_cast<uint8_t*>(fData), sizeof(fData));
                    SkSHA1::Digest digest;
                    sha1.finish(digest);
                }
            } break;
75
            case kMurmur3_ChecksumType: {
76
                for (int i = 0; i < this->getLoops(); i++) {
77 78 79 80
                    volatile uint32_t result = SkChecksum::Murmur3(fData, sizeof(fData));
                    sk_ignore_unused_variable(result);
                }
            }break;
81
        }
82

83 84 85 86 87 88 89 90
    }

private:
    typedef SkBenchmark INHERITED;
};

///////////////////////////////////////////////////////////////////////////////

91 92 93 94
DEF_BENCH( return new ComputeChecksumBench(kChecksum_ChecksumType); )
DEF_BENCH( return new ComputeChecksumBench(kMD5_ChecksumType); )
DEF_BENCH( return new ComputeChecksumBench(kSHA1_ChecksumType); )
DEF_BENCH( return new ComputeChecksumBench(kMurmur3_ChecksumType); )