]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - android-sdk/platform-bionic.git/blob - benchmarks/string_benchmark.cpp
Merge "Sync with upstream for gethnamaddr.c."
[android-sdk/platform-bionic.git] / benchmarks / string_benchmark.cpp
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  */
17 #include "benchmark.h"
19 #include <string.h>
21 #define KB 1024
22 #define MB 1024*KB
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)
27 // TODO: test unaligned operation too? (currently everything will be 8-byte aligned by malloc.)
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();
36   volatile int c __attribute__((unused)) = 0;
37   for (int i = 0; i < iters; ++i) {
38     c += memcmp(dst, src, nbytes);
39   }
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;
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();
54   for (int i = 0; i < iters; ++i) {
55     memcpy(dst, src, nbytes);
56   }
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;
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();
71   for (int i = 0; i < iters; ++i) {
72     memmove(buf, buf + 1, nbytes); // Worst-case overlap.
73   }
75   StopBenchmarkTiming();
76   SetBenchmarkBytesProcessed(int64_t(iters) * int64_t(nbytes));
77   delete[] buf;
78 }
79 BENCHMARK(BM_string_memmove)->AT_COMMON_SIZES;
81 static void BM_string_memset(int iters, int nbytes) {
82   StopBenchmarkTiming();
83   char* dst = new char[nbytes];
84   StartBenchmarkTiming();
86   for (int i = 0; i < iters; ++i) {
87     memset(dst, 0, nbytes);
88   }
90   StopBenchmarkTiming();
91   SetBenchmarkBytesProcessed(int64_t(iters) * int64_t(nbytes));
92   delete[] dst;
93 }
94 BENCHMARK(BM_string_memset)->AT_COMMON_SIZES;
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();
103   volatile int c __attribute__((unused)) = 0;
104   for (int i = 0; i < iters; ++i) {
105     c += strlen(s);
106   }
108   StopBenchmarkTiming();
109   SetBenchmarkBytesProcessed(int64_t(iters) * int64_t(nbytes));
110   delete[] s;
112 BENCHMARK(BM_string_strlen)->AT_COMMON_SIZES;