blob: 0a385126573588745e872a0e64ad8befcccfa0a8 [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
Elliott Hughes281e06b2016-02-17 10:23:52 -080020#include <benchmark/benchmark.h>
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080021
Chih-Hung Hsieh1a5fd9c2016-06-10 11:07:21 -070022constexpr auto KB = 1024;
Elliott Hughes7be369d2012-11-08 15:37:43 -080023
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 Hughes281e06b2016-02-17 10:23:52 -080029static void BM_string_memcmp(benchmark::State& state) {
30 const size_t nbytes = state.range_x();
Elliott Hughes7be369d2012-11-08 15:37:43 -080031 char* src = new char[nbytes]; char* dst = new char[nbytes];
32 memset(src, 'x', nbytes);
33 memset(dst, 'x', nbytes);
Elliott Hughes7be369d2012-11-08 15:37:43 -080034
35 volatile int c __attribute__((unused)) = 0;
Elliott Hughes281e06b2016-02-17 10:23:52 -080036 while (state.KeepRunning()) {
Elliott Hughes7be369d2012-11-08 15:37:43 -080037 c += memcmp(dst, src, nbytes);
38 }
39
Elliott Hughes281e06b2016-02-17 10:23:52 -080040 state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes));
Elliott Hughes7be369d2012-11-08 15:37:43 -080041 delete[] src;
42 delete[] dst;
43}
Elliott Hughes281e06b2016-02-17 10:23:52 -080044BENCHMARK(BM_string_memcmp)->AT_COMMON_SIZES;
Elliott Hughes7be369d2012-11-08 15:37:43 -080045
Elliott Hughes281e06b2016-02-17 10:23:52 -080046static void BM_string_memcpy(benchmark::State& state) {
47 const size_t nbytes = state.range_x();
Elliott Hughes7be369d2012-11-08 15:37:43 -080048 char* src = new char[nbytes]; char* dst = new char[nbytes];
49 memset(src, 'x', nbytes);
Elliott Hughes7be369d2012-11-08 15:37:43 -080050
Elliott Hughes281e06b2016-02-17 10:23:52 -080051 while (state.KeepRunning()) {
Elliott Hughes7be369d2012-11-08 15:37:43 -080052 memcpy(dst, src, nbytes);
53 }
54
Elliott Hughes281e06b2016-02-17 10:23:52 -080055 state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes));
Elliott Hughes7be369d2012-11-08 15:37:43 -080056 delete[] src;
57 delete[] dst;
58}
Elliott Hughes281e06b2016-02-17 10:23:52 -080059BENCHMARK(BM_string_memcpy)->AT_COMMON_SIZES;
Elliott Hughes7be369d2012-11-08 15:37:43 -080060
Elliott Hughes281e06b2016-02-17 10:23:52 -080061static void BM_string_memmove(benchmark::State& state) {
62 const size_t nbytes = state.range_x();
Elliott Hughesfbe44ec2012-11-09 14:59:21 -080063 char* buf = new char[nbytes + 64];
64 memset(buf, 'x', nbytes + 64);
Elliott Hughesfbe44ec2012-11-09 14:59:21 -080065
Elliott Hughes281e06b2016-02-17 10:23:52 -080066 while (state.KeepRunning()) {
Elliott Hughesfbe44ec2012-11-09 14:59:21 -080067 memmove(buf, buf + 1, nbytes); // Worst-case overlap.
68 }
69
Elliott Hughes281e06b2016-02-17 10:23:52 -080070 state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes));
Elliott Hughesfbe44ec2012-11-09 14:59:21 -080071 delete[] buf;
72}
Elliott Hughes281e06b2016-02-17 10:23:52 -080073BENCHMARK(BM_string_memmove)->AT_COMMON_SIZES;
Elliott Hughesfbe44ec2012-11-09 14:59:21 -080074
Elliott Hughes281e06b2016-02-17 10:23:52 -080075static void BM_string_memset(benchmark::State& state) {
76 const size_t nbytes = state.range_x();
Elliott Hughes7be369d2012-11-08 15:37:43 -080077 char* dst = new char[nbytes];
Elliott Hughes7be369d2012-11-08 15:37:43 -080078
Elliott Hughes281e06b2016-02-17 10:23:52 -080079 while (state.KeepRunning()) {
Elliott Hughes7be369d2012-11-08 15:37:43 -080080 memset(dst, 0, nbytes);
81 }
82
Elliott Hughes281e06b2016-02-17 10:23:52 -080083 state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes));
Elliott Hughes7be369d2012-11-08 15:37:43 -080084 delete[] dst;
85}
Elliott Hughes281e06b2016-02-17 10:23:52 -080086BENCHMARK(BM_string_memset)->AT_COMMON_SIZES;
Elliott Hughes7be369d2012-11-08 15:37:43 -080087
Elliott Hughes281e06b2016-02-17 10:23:52 -080088static void BM_string_strlen(benchmark::State& state) {
89 const size_t nbytes = state.range_x();
Elliott Hughes7be369d2012-11-08 15:37:43 -080090 char* s = new char[nbytes];
91 memset(s, 'x', nbytes);
92 s[nbytes - 1] = 0;
Elliott Hughes7be369d2012-11-08 15:37:43 -080093
94 volatile int c __attribute__((unused)) = 0;
Elliott Hughes281e06b2016-02-17 10:23:52 -080095 while (state.KeepRunning()) {
Elliott Hughes7be369d2012-11-08 15:37:43 -080096 c += strlen(s);
97 }
98
Elliott Hughes281e06b2016-02-17 10:23:52 -080099 state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes));
Elliott Hughes7be369d2012-11-08 15:37:43 -0800100 delete[] s;
101}
Elliott Hughes281e06b2016-02-17 10:23:52 -0800102BENCHMARK(BM_string_strlen)->AT_COMMON_SIZES;