blob: 866aa00473f65f3668c43c5c768662924e553ef5 [file] [log] [blame]
Elliott Hughes7be369d2012-11-08 15:37:43 -08001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080017#include <stdint.h>
Elliott Hughes7be369d2012-11-08 15:37:43 -080018#include <string.h>
19
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080020#include <benchmark/Benchmark.h>
21
Elliott Hughes7be369d2012-11-08 15:37:43 -080022#define KB 1024
23#define MB 1024*KB
24
25#define AT_COMMON_SIZES \
26 Arg(8)->Arg(64)->Arg(512)->Arg(1*KB)->Arg(8*KB)->Arg(16*KB)->Arg(32*KB)->Arg(64*KB)
27
28// TODO: test unaligned operation too? (currently everything will be 8-byte aligned by malloc.)
29
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080030BENCHMARK_WITH_ARG(BM_string_memcmp, int)->AT_COMMON_SIZES;
31void BM_string_memcmp::Run(int iters, int nbytes) {
Elliott Hughes7be369d2012-11-08 15:37:43 -080032 StopBenchmarkTiming();
33 char* src = new char[nbytes]; char* dst = new char[nbytes];
34 memset(src, 'x', nbytes);
35 memset(dst, 'x', nbytes);
36 StartBenchmarkTiming();
37
38 volatile int c __attribute__((unused)) = 0;
Elliott Hughes9edb3e02013-02-06 15:47:09 -080039 for (int i = 0; i < iters; ++i) {
Elliott Hughes7be369d2012-11-08 15:37:43 -080040 c += memcmp(dst, src, nbytes);
41 }
42
43 StopBenchmarkTiming();
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080044 SetBenchmarkBytesProcessed(uint64_t(iters) * uint64_t(nbytes));
Elliott Hughes7be369d2012-11-08 15:37:43 -080045 delete[] src;
46 delete[] dst;
47}
Elliott Hughes7be369d2012-11-08 15:37:43 -080048
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080049BENCHMARK_WITH_ARG(BM_string_memcpy, int)->AT_COMMON_SIZES;
50void BM_string_memcpy::Run(int iters, int nbytes) {
Elliott Hughes7be369d2012-11-08 15:37:43 -080051 StopBenchmarkTiming();
52 char* src = new char[nbytes]; char* dst = new char[nbytes];
53 memset(src, 'x', nbytes);
54 StartBenchmarkTiming();
55
Elliott Hughes9edb3e02013-02-06 15:47:09 -080056 for (int i = 0; i < iters; ++i) {
Elliott Hughes7be369d2012-11-08 15:37:43 -080057 memcpy(dst, src, nbytes);
58 }
59
60 StopBenchmarkTiming();
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080061 SetBenchmarkBytesProcessed(uint64_t(iters) * uint64_t(nbytes));
Elliott Hughes7be369d2012-11-08 15:37:43 -080062 delete[] src;
63 delete[] dst;
64}
Elliott Hughes7be369d2012-11-08 15:37:43 -080065
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080066BENCHMARK_WITH_ARG(BM_string_memmove, int)->AT_COMMON_SIZES;
67void BM_string_memmove::Run(int iters, int nbytes) {
Elliott Hughesfbe44ec2012-11-09 14:59:21 -080068 StopBenchmarkTiming();
69 char* buf = new char[nbytes + 64];
70 memset(buf, 'x', nbytes + 64);
71 StartBenchmarkTiming();
72
Elliott Hughes9edb3e02013-02-06 15:47:09 -080073 for (int i = 0; i < iters; ++i) {
Elliott Hughesfbe44ec2012-11-09 14:59:21 -080074 memmove(buf, buf + 1, nbytes); // Worst-case overlap.
75 }
76
77 StopBenchmarkTiming();
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080078 SetBenchmarkBytesProcessed(uint64_t(iters) * uint64_t(nbytes));
Elliott Hughesfbe44ec2012-11-09 14:59:21 -080079 delete[] buf;
80}
Elliott Hughesfbe44ec2012-11-09 14:59:21 -080081
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080082BENCHMARK_WITH_ARG(BM_string_memset, int)->AT_COMMON_SIZES;
83void BM_string_memset::Run(int iters, int nbytes) {
Elliott Hughes7be369d2012-11-08 15:37:43 -080084 StopBenchmarkTiming();
85 char* dst = new char[nbytes];
86 StartBenchmarkTiming();
87
Elliott Hughes9edb3e02013-02-06 15:47:09 -080088 for (int i = 0; i < iters; ++i) {
Elliott Hughes7be369d2012-11-08 15:37:43 -080089 memset(dst, 0, nbytes);
90 }
91
92 StopBenchmarkTiming();
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080093 SetBenchmarkBytesProcessed(uint64_t(iters) * uint64_t(nbytes));
Elliott Hughes7be369d2012-11-08 15:37:43 -080094 delete[] dst;
95}
Elliott Hughes7be369d2012-11-08 15:37:43 -080096
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080097BENCHMARK_WITH_ARG(BM_string_strlen, int)->AT_COMMON_SIZES;
98void BM_string_strlen::Run(int iters, int nbytes) {
Elliott Hughes7be369d2012-11-08 15:37:43 -080099 StopBenchmarkTiming();
100 char* s = new char[nbytes];
101 memset(s, 'x', nbytes);
102 s[nbytes - 1] = 0;
103 StartBenchmarkTiming();
104
105 volatile int c __attribute__((unused)) = 0;
Elliott Hughes9edb3e02013-02-06 15:47:09 -0800106 for (int i = 0; i < iters; ++i) {
Elliott Hughes7be369d2012-11-08 15:37:43 -0800107 c += strlen(s);
108 }
109
110 StopBenchmarkTiming();
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800111 SetBenchmarkBytesProcessed(uint64_t(iters) * uint64_t(nbytes));
Elliott Hughes7be369d2012-11-08 15:37:43 -0800112 delete[] s;
113}