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_string_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_string_memcmp)->AT_COMMON_SIZES; 47 48 static void BM_string_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_string_memcpy)->AT_COMMON_SIZES; 64 65 static void BM_string_memmove(int iters, int nbytes) { 66 StopBenchmarkTiming(); 67 char* buf = new char[nbytes + 64]; 68 memset(buf, 'x', nbytes + 64); 69 StartBenchmarkTiming(); 70 71 for (int i = 0; i < iters; ++i) { 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 } 79 BENCHMARK(BM_string_memmove)->AT_COMMON_SIZES; 80 81 static void BM_string_memset(int iters, int nbytes) { 82 StopBenchmarkTiming(); 83 char* dst = new char[nbytes]; 84 StartBenchmarkTiming(); 85 86 for (int i = 0; i < iters; ++i) { 87 memset(dst, 0, nbytes); 88 } 89 90 StopBenchmarkTiming(); 91 SetBenchmarkBytesProcessed(int64_t(iters) * int64_t(nbytes)); 92 delete[] dst; 93 } 94 BENCHMARK(BM_string_memset)->AT_COMMON_SIZES; 95 96 static void BM_string_strlen(int iters, int nbytes) { 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; 104 for (int i = 0; i < iters; ++i) { 105 c += strlen(s); 106 } 107 108 StopBenchmarkTiming(); 109 SetBenchmarkBytesProcessed(int64_t(iters) * int64_t(nbytes)); 110 delete[] s; 111 } 112 BENCHMARK(BM_string_strlen)->AT_COMMON_SIZES; 113