blob: 536e253fde762f01c8076c02295201b6573c0a77 [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
17#include "benchmark.h"
18
19#include <string.h>
20
21#define KB 1024
22#define MB 1024*KB
23
24#define AT_COMMON_SIZES \
25 Arg(8)->Arg(64)->Arg(512)->Arg(1*KB)->Arg(8*KB)->Arg(16*KB)->Arg(32*KB)->Arg(64*KB)
26
27// TODO: test unaligned operation too? (currently everything will be 8-byte aligned by malloc.)
28
Elliott Hughes9edb3e02013-02-06 15:47:09 -080029static void BM_string_memcmp(int iters, int nbytes) {
Elliott Hughes7be369d2012-11-08 15:37:43 -080030 StopBenchmarkTiming();
31 char* src = new char[nbytes]; char* dst = new char[nbytes];
32 memset(src, 'x', nbytes);
33 memset(dst, 'x', nbytes);
34 StartBenchmarkTiming();
35
36 volatile int c __attribute__((unused)) = 0;
Elliott Hughes9edb3e02013-02-06 15:47:09 -080037 for (int i = 0; i < iters; ++i) {
Elliott Hughes7be369d2012-11-08 15:37:43 -080038 c += memcmp(dst, src, nbytes);
39 }
40
41 StopBenchmarkTiming();
42 SetBenchmarkBytesProcessed(int64_t(iters) * int64_t(nbytes));
43 delete[] src;
44 delete[] dst;
45}
Elliott Hughes9edb3e02013-02-06 15:47:09 -080046BENCHMARK(BM_string_memcmp)->AT_COMMON_SIZES;
Elliott Hughes7be369d2012-11-08 15:37:43 -080047
Elliott Hughes9edb3e02013-02-06 15:47:09 -080048static void BM_string_memcpy(int iters, int nbytes) {
Elliott Hughes7be369d2012-11-08 15:37:43 -080049 StopBenchmarkTiming();
50 char* src = new char[nbytes]; char* dst = new char[nbytes];
51 memset(src, 'x', nbytes);
52 StartBenchmarkTiming();
53
Elliott Hughes9edb3e02013-02-06 15:47:09 -080054 for (int i = 0; i < iters; ++i) {
Elliott Hughes7be369d2012-11-08 15:37:43 -080055 memcpy(dst, src, nbytes);
56 }
57
58 StopBenchmarkTiming();
59 SetBenchmarkBytesProcessed(int64_t(iters) * int64_t(nbytes));
60 delete[] src;
61 delete[] dst;
62}
Elliott Hughes9edb3e02013-02-06 15:47:09 -080063BENCHMARK(BM_string_memcpy)->AT_COMMON_SIZES;
Elliott Hughes7be369d2012-11-08 15:37:43 -080064
Elliott Hughes9edb3e02013-02-06 15:47:09 -080065static void BM_string_memmove(int iters, int nbytes) {
Elliott Hughesfbe44ec2012-11-09 14:59:21 -080066 StopBenchmarkTiming();
67 char* buf = new char[nbytes + 64];
68 memset(buf, 'x', nbytes + 64);
69 StartBenchmarkTiming();
70
Elliott Hughes9edb3e02013-02-06 15:47:09 -080071 for (int i = 0; i < iters; ++i) {
Elliott Hughesfbe44ec2012-11-09 14:59:21 -080072 memmove(buf, buf + 1, nbytes); // Worst-case overlap.
73 }
74
75 StopBenchmarkTiming();
76 SetBenchmarkBytesProcessed(int64_t(iters) * int64_t(nbytes));
77 delete[] buf;
78}
Elliott Hughes9edb3e02013-02-06 15:47:09 -080079BENCHMARK(BM_string_memmove)->AT_COMMON_SIZES;
Elliott Hughesfbe44ec2012-11-09 14:59:21 -080080
Elliott Hughes9edb3e02013-02-06 15:47:09 -080081static void BM_string_memset(int iters, int nbytes) {
Elliott Hughes7be369d2012-11-08 15:37:43 -080082 StopBenchmarkTiming();
83 char* dst = new char[nbytes];
84 StartBenchmarkTiming();
85
Elliott Hughes9edb3e02013-02-06 15:47:09 -080086 for (int i = 0; i < iters; ++i) {
Elliott Hughes7be369d2012-11-08 15:37:43 -080087 memset(dst, 0, nbytes);
88 }
89
90 StopBenchmarkTiming();
91 SetBenchmarkBytesProcessed(int64_t(iters) * int64_t(nbytes));
92 delete[] dst;
93}
Elliott Hughes9edb3e02013-02-06 15:47:09 -080094BENCHMARK(BM_string_memset)->AT_COMMON_SIZES;
Elliott Hughes7be369d2012-11-08 15:37:43 -080095
Elliott Hughes9edb3e02013-02-06 15:47:09 -080096static void BM_string_strlen(int iters, int nbytes) {
Elliott Hughes7be369d2012-11-08 15:37:43 -080097 StopBenchmarkTiming();
98 char* s = new char[nbytes];
99 memset(s, 'x', nbytes);
100 s[nbytes - 1] = 0;
101 StartBenchmarkTiming();
102
103 volatile int c __attribute__((unused)) = 0;
Elliott Hughes9edb3e02013-02-06 15:47:09 -0800104 for (int i = 0; i < iters; ++i) {
Elliott Hughes7be369d2012-11-08 15:37:43 -0800105 c += strlen(s);
106 }
107
108 StopBenchmarkTiming();
109 SetBenchmarkBytesProcessed(int64_t(iters) * int64_t(nbytes));
110 delete[] s;
111}
Elliott Hughes9edb3e02013-02-06 15:47:09 -0800112BENCHMARK(BM_string_strlen)->AT_COMMON_SIZES;