| 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 |  | 
| Elliott Hughes | 9edb3e0 | 2013-02-06 15:47:09 -0800 | [diff] [blame] | 29 | static void BM_string_memcmp(int iters, int nbytes) { | 
| Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 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; | 
| Elliott Hughes | 9edb3e0 | 2013-02-06 15:47:09 -0800 | [diff] [blame] | 37 | for (int i = 0; i < iters; ++i) { | 
| Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 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 | } | 
| Elliott Hughes | 9edb3e0 | 2013-02-06 15:47:09 -0800 | [diff] [blame] | 46 | BENCHMARK(BM_string_memcmp)->AT_COMMON_SIZES; | 
| Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 47 |  | 
| Elliott Hughes | 9edb3e0 | 2013-02-06 15:47:09 -0800 | [diff] [blame] | 48 | static void BM_string_memcpy(int iters, int nbytes) { | 
| Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 49 | StopBenchmarkTiming(); | 
|  | 50 | char* src = new char[nbytes]; char* dst = new char[nbytes]; | 
|  | 51 | memset(src, 'x', nbytes); | 
|  | 52 | StartBenchmarkTiming(); | 
|  | 53 |  | 
| Elliott Hughes | 9edb3e0 | 2013-02-06 15:47:09 -0800 | [diff] [blame] | 54 | for (int i = 0; i < iters; ++i) { | 
| Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 55 | memcpy(dst, src, nbytes); | 
|  | 56 | } | 
|  | 57 |  | 
|  | 58 | StopBenchmarkTiming(); | 
|  | 59 | SetBenchmarkBytesProcessed(int64_t(iters) * int64_t(nbytes)); | 
|  | 60 | delete[] src; | 
|  | 61 | delete[] dst; | 
|  | 62 | } | 
| Elliott Hughes | 9edb3e0 | 2013-02-06 15:47:09 -0800 | [diff] [blame] | 63 | BENCHMARK(BM_string_memcpy)->AT_COMMON_SIZES; | 
| Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 64 |  | 
| Elliott Hughes | 9edb3e0 | 2013-02-06 15:47:09 -0800 | [diff] [blame] | 65 | static void BM_string_memmove(int iters, int nbytes) { | 
| Elliott Hughes | fbe44ec | 2012-11-09 14:59:21 -0800 | [diff] [blame] | 66 | StopBenchmarkTiming(); | 
|  | 67 | char* buf = new char[nbytes + 64]; | 
|  | 68 | memset(buf, 'x', nbytes + 64); | 
|  | 69 | StartBenchmarkTiming(); | 
|  | 70 |  | 
| Elliott Hughes | 9edb3e0 | 2013-02-06 15:47:09 -0800 | [diff] [blame] | 71 | for (int i = 0; i < iters; ++i) { | 
| Elliott Hughes | fbe44ec | 2012-11-09 14:59:21 -0800 | [diff] [blame] | 72 | 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 Hughes | 9edb3e0 | 2013-02-06 15:47:09 -0800 | [diff] [blame] | 79 | BENCHMARK(BM_string_memmove)->AT_COMMON_SIZES; | 
| Elliott Hughes | fbe44ec | 2012-11-09 14:59:21 -0800 | [diff] [blame] | 80 |  | 
| Elliott Hughes | 9edb3e0 | 2013-02-06 15:47:09 -0800 | [diff] [blame] | 81 | static void BM_string_memset(int iters, int nbytes) { | 
| Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 82 | StopBenchmarkTiming(); | 
|  | 83 | char* dst = new char[nbytes]; | 
|  | 84 | StartBenchmarkTiming(); | 
|  | 85 |  | 
| Elliott Hughes | 9edb3e0 | 2013-02-06 15:47:09 -0800 | [diff] [blame] | 86 | for (int i = 0; i < iters; ++i) { | 
| Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 87 | memset(dst, 0, nbytes); | 
|  | 88 | } | 
|  | 89 |  | 
|  | 90 | StopBenchmarkTiming(); | 
|  | 91 | SetBenchmarkBytesProcessed(int64_t(iters) * int64_t(nbytes)); | 
|  | 92 | delete[] dst; | 
|  | 93 | } | 
| Elliott Hughes | 9edb3e0 | 2013-02-06 15:47:09 -0800 | [diff] [blame] | 94 | BENCHMARK(BM_string_memset)->AT_COMMON_SIZES; | 
| Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 95 |  | 
| Elliott Hughes | 9edb3e0 | 2013-02-06 15:47:09 -0800 | [diff] [blame] | 96 | static void BM_string_strlen(int iters, int nbytes) { | 
| Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 97 | 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 Hughes | 9edb3e0 | 2013-02-06 15:47:09 -0800 | [diff] [blame] | 104 | for (int i = 0; i < iters; ++i) { | 
| Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 105 | c += strlen(s); | 
|  | 106 | } | 
|  | 107 |  | 
|  | 108 | StopBenchmarkTiming(); | 
|  | 109 | SetBenchmarkBytesProcessed(int64_t(iters) * int64_t(nbytes)); | 
|  | 110 | delete[] s; | 
|  | 111 | } | 
| Elliott Hughes | 9edb3e0 | 2013-02-06 15:47:09 -0800 | [diff] [blame] | 112 | BENCHMARK(BM_string_strlen)->AT_COMMON_SIZES; |