| Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 1 | /* | 
|  | 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 Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 17 | #include <stdint.h> | 
| Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 18 | #include <string.h> | 
|  | 19 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 20 | #include <benchmark/benchmark.h> | 
| Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 21 |  | 
| Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 22 | #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 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 30 | static void BM_string_memcmp(benchmark::State& state) { | 
|  | 31 | const size_t nbytes = state.range_x(); | 
| Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 32 | char* src = new char[nbytes]; char* dst = new char[nbytes]; | 
|  | 33 | memset(src, 'x', nbytes); | 
|  | 34 | memset(dst, 'x', nbytes); | 
| Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 35 |  | 
|  | 36 | volatile int c __attribute__((unused)) = 0; | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 37 | while (state.KeepRunning()) { | 
| Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 38 | c += memcmp(dst, src, nbytes); | 
|  | 39 | } | 
|  | 40 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 41 | state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes)); | 
| Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 42 | delete[] src; | 
|  | 43 | delete[] dst; | 
|  | 44 | } | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 45 | BENCHMARK(BM_string_memcmp)->AT_COMMON_SIZES; | 
| Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 46 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 47 | static void BM_string_memcpy(benchmark::State& state) { | 
|  | 48 | const size_t nbytes = state.range_x(); | 
| Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 49 | char* src = new char[nbytes]; char* dst = new char[nbytes]; | 
|  | 50 | memset(src, 'x', nbytes); | 
| Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 51 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 52 | while (state.KeepRunning()) { | 
| Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 53 | memcpy(dst, src, nbytes); | 
|  | 54 | } | 
|  | 55 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 56 | state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes)); | 
| Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 57 | delete[] src; | 
|  | 58 | delete[] dst; | 
|  | 59 | } | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 60 | BENCHMARK(BM_string_memcpy)->AT_COMMON_SIZES; | 
| Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 61 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 62 | static void BM_string_memmove(benchmark::State& state) { | 
|  | 63 | const size_t nbytes = state.range_x(); | 
| Elliott Hughes | fbe44ec | 2012-11-09 14:59:21 -0800 | [diff] [blame] | 64 | char* buf = new char[nbytes + 64]; | 
|  | 65 | memset(buf, 'x', nbytes + 64); | 
| Elliott Hughes | fbe44ec | 2012-11-09 14:59:21 -0800 | [diff] [blame] | 66 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 67 | while (state.KeepRunning()) { | 
| Elliott Hughes | fbe44ec | 2012-11-09 14:59:21 -0800 | [diff] [blame] | 68 | memmove(buf, buf + 1, nbytes); // Worst-case overlap. | 
|  | 69 | } | 
|  | 70 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 71 | state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes)); | 
| Elliott Hughes | fbe44ec | 2012-11-09 14:59:21 -0800 | [diff] [blame] | 72 | delete[] buf; | 
|  | 73 | } | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 74 | BENCHMARK(BM_string_memmove)->AT_COMMON_SIZES; | 
| Elliott Hughes | fbe44ec | 2012-11-09 14:59:21 -0800 | [diff] [blame] | 75 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 76 | static void BM_string_memset(benchmark::State& state) { | 
|  | 77 | const size_t nbytes = state.range_x(); | 
| Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 78 | char* dst = new char[nbytes]; | 
| Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 79 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 80 | while (state.KeepRunning()) { | 
| Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 81 | memset(dst, 0, nbytes); | 
|  | 82 | } | 
|  | 83 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 84 | state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes)); | 
| Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 85 | delete[] dst; | 
|  | 86 | } | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 87 | BENCHMARK(BM_string_memset)->AT_COMMON_SIZES; | 
| Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 88 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 89 | static void BM_string_strlen(benchmark::State& state) { | 
|  | 90 | const size_t nbytes = state.range_x(); | 
| Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 91 | char* s = new char[nbytes]; | 
|  | 92 | memset(s, 'x', nbytes); | 
|  | 93 | s[nbytes - 1] = 0; | 
| Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 94 |  | 
|  | 95 | volatile int c __attribute__((unused)) = 0; | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 96 | while (state.KeepRunning()) { | 
| Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 97 | c += strlen(s); | 
|  | 98 | } | 
|  | 99 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 100 | state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes)); | 
| Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 101 | delete[] s; | 
|  | 102 | } | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 103 | BENCHMARK(BM_string_strlen)->AT_COMMON_SIZES; |