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 | |
| 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 | |
| 29 | static void BM_memcmp(int iters, int nbytes) { |
| 30 | 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; |
| 37 | for (int i = 0; i < iters; i++) { |
| 38 | c += memcmp(dst, src, nbytes); |
| 39 | } |
| 40 | |
| 41 | StopBenchmarkTiming(); |
| 42 | SetBenchmarkBytesProcessed(int64_t(iters) * int64_t(nbytes)); |
| 43 | delete[] src; |
| 44 | delete[] dst; |
| 45 | } |
| 46 | BENCHMARK(BM_memcmp)->AT_COMMON_SIZES; |
| 47 | |
| 48 | static void BM_memcpy(int iters, int nbytes) { |
| 49 | StopBenchmarkTiming(); |
| 50 | char* src = new char[nbytes]; char* dst = new char[nbytes]; |
| 51 | memset(src, 'x', nbytes); |
| 52 | StartBenchmarkTiming(); |
| 53 | |
| 54 | for (int i = 0; i < iters; i++) { |
| 55 | memcpy(dst, src, nbytes); |
| 56 | } |
| 57 | |
| 58 | StopBenchmarkTiming(); |
| 59 | SetBenchmarkBytesProcessed(int64_t(iters) * int64_t(nbytes)); |
| 60 | delete[] src; |
| 61 | delete[] dst; |
| 62 | } |
| 63 | BENCHMARK(BM_memcpy)->AT_COMMON_SIZES; |
| 64 | |
| 65 | static void BM_memset(int iters, int nbytes) { |
| 66 | StopBenchmarkTiming(); |
| 67 | char* dst = new char[nbytes]; |
| 68 | StartBenchmarkTiming(); |
| 69 | |
| 70 | for (int i = 0; i < iters; i++) { |
| 71 | memset(dst, 0, nbytes); |
| 72 | } |
| 73 | |
| 74 | StopBenchmarkTiming(); |
| 75 | SetBenchmarkBytesProcessed(int64_t(iters) * int64_t(nbytes)); |
| 76 | delete[] dst; |
| 77 | } |
| 78 | BENCHMARK(BM_memset)->AT_COMMON_SIZES; |
| 79 | |
| 80 | static void BM_strlen(int iters, int nbytes) { |
| 81 | StopBenchmarkTiming(); |
| 82 | char* s = new char[nbytes]; |
| 83 | memset(s, 'x', nbytes); |
| 84 | s[nbytes - 1] = 0; |
| 85 | StartBenchmarkTiming(); |
| 86 | |
| 87 | volatile int c __attribute__((unused)) = 0; |
| 88 | for (int i = 0; i < iters; i++) { |
| 89 | c += strlen(s); |
| 90 | } |
| 91 | |
| 92 | StopBenchmarkTiming(); |
| 93 | SetBenchmarkBytesProcessed(int64_t(iters) * int64_t(nbytes)); |
| 94 | delete[] s; |
| 95 | } |
| 96 | BENCHMARK(BM_strlen)->AT_COMMON_SIZES; |