Home | History | Annotate | Download | only in benchmarks
      1 /*
      2  * Copyright (C) 2017 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 <err.h>
     18 #include <langinfo.h>
     19 #include <locale.h>
     20 #include <stdlib.h>
     21 
     22 #include <benchmark/benchmark.h>
     23 #include "util.h"
     24 
     25 static void BM_stdlib_malloc_free(benchmark::State& state) {
     26   const size_t nbytes = state.range(0);
     27 
     28   void* c;
     29   while (state.KeepRunning()) {
     30     c = malloc(nbytes);
     31     free(c);
     32   }
     33 
     34   state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes));
     35 }
     36 BIONIC_BENCHMARK_WITH_ARG(BM_stdlib_malloc_free, "AT_COMMON_SIZES");
     37 
     38 static void BM_stdlib_mbstowcs(benchmark::State& state) {
     39   const size_t buf_alignment = state.range(0);
     40   const size_t widebuf_alignment = state.range(1);
     41 
     42   std::vector<char> buf;
     43   std::vector<wchar_t> widebuf;
     44 
     45   setlocale(LC_CTYPE, "C.UTF-8")
     46   || setlocale(LC_CTYPE, "en_US.UTF-8")
     47   || setlocale(LC_CTYPE, "en_GB.UTF-8")
     48   || setlocale(LC_CTYPE, "en.UTF-8")
     49   || setlocale(LC_CTYPE, "de_DE-8")
     50   || setlocale(LC_CTYPE, "fr_FR-8");
     51   if (strcmp(nl_langinfo(CODESET), "UTF-8")) {
     52     errx(1, "ERROR: unable to set locale in BM_stdlib_mbstowcs");
     53   }
     54 
     55   char* buf_aligned = GetAlignedPtr(&buf, buf_alignment, 500000);
     56   wchar_t* widebuf_aligned = GetAlignedPtr(&widebuf, widebuf_alignment, 500000);
     57   size_t i, j, k, l;
     58   l = 0;
     59   for (i=0xc3; i<0xe0; i++)
     60     for (j=0x80; j<0xc0; j++)
     61       buf[l++] = i, buf[l++] = j;
     62   for (i=0xe1; i<0xed; i++)
     63     for (j=0x80; j<0xc0; j++)
     64       for (k=0x80; k<0xc0; k++)
     65         buf[l++] = i, buf[l++] = j, buf[l++] = k;
     66   for (i=0xf1; i<0xf4; i++)
     67     for (j=0x80; j<0xc0; j++)
     68       for (k=0x80; k<0xc0; k++)
     69         buf[l++] = i, buf[l++] = j, buf[l++] = 0x80, buf[l++] = k;
     70   buf[l++] = 0;
     71 
     72   volatile size_t c __attribute__((unused)) = 0;
     73   while (state.KeepRunning()) {
     74     c = mbstowcs(widebuf_aligned, buf_aligned, 500000);
     75   }
     76 
     77   state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(500000));
     78 }
     79 BIONIC_BENCHMARK_WITH_ARG(BM_stdlib_mbstowcs, "0 0");
     80 
     81 static void BM_stdlib_mbrtowc(benchmark::State& state) {
     82   const size_t buf_alignment = state.range(0);
     83 
     84   std::vector<char> buf;
     85 
     86   setlocale(LC_CTYPE, "C.UTF-8")
     87   || setlocale(LC_CTYPE, "en_US.UTF-8")
     88   || setlocale(LC_CTYPE, "en_GB.UTF-8")
     89   || setlocale(LC_CTYPE, "en.UTF-8")
     90   || setlocale(LC_CTYPE, "de_DE-8")
     91   || setlocale(LC_CTYPE, "fr_FR-8");
     92   if (strcmp(nl_langinfo(CODESET), "UTF-8")) {
     93     errx(1, "ERROR: unable to set locale in BM_stdlib_mbrtowc");
     94   }
     95 
     96   char* buf_aligned = GetAlignedPtr(&buf, buf_alignment, 500000);
     97   size_t i, j, k, l;
     98   l = 0;
     99   for (i=0xc3; i<0xe0; i++)
    100     for (j=0x80; j<0xc0; j++)
    101       buf[l++] = i, buf[l++] = j;
    102   for (i=0xe1; i<0xed; i++)
    103     for (j=0x80; j<0xc0; j++)
    104       for (k=0x80; k<0xc0; k++)
    105         buf[l++] = i, buf[l++] = j, buf[l++] = k;
    106   for (i=0xf1; i<0xf4; i++)
    107     for (j=0x80; j<0xc0; j++)
    108       for (k=0x80; k<0xc0; k++)
    109         buf[l++] = i, buf[l++] = j, buf[l++] = 0x80, buf[l++] = k;
    110   buf[l++] = 0;
    111 
    112   wchar_t wc = 0;
    113   while (state.KeepRunning()) {
    114     for (j = 0; buf_aligned[j]; j+=mbrtowc(&wc, buf_aligned + j, 4, NULL)) {
    115     }
    116   }
    117 
    118   state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(500000));
    119 }
    120 BIONIC_BENCHMARK_WITH_ARG(BM_stdlib_mbrtowc, "0");
    121 
    122 void BM_stdlib_atoi(benchmark::State& state) {
    123   while (state.KeepRunning()) {
    124     benchmark::DoNotOptimize(atoi(" -123"));
    125   }
    126 }
    127 BIONIC_BENCHMARK(BM_stdlib_atoi);
    128 
    129 void BM_stdlib_atol(benchmark::State& state) {
    130   while (state.KeepRunning()) {
    131     benchmark::DoNotOptimize(atol(" -123"));
    132   }
    133 }
    134 BIONIC_BENCHMARK(BM_stdlib_atol);
    135 
    136 void BM_stdlib_strtol(benchmark::State& state) {
    137   while (state.KeepRunning()) {
    138     benchmark::DoNotOptimize(strtol(" -123", nullptr, 0));
    139   }
    140 }
    141 BIONIC_BENCHMARK(BM_stdlib_strtol);
    142 
    143 void BM_stdlib_strtoll(benchmark::State& state) {
    144   while (state.KeepRunning()) {
    145     benchmark::DoNotOptimize(strtoll(" -123", nullptr, 0));
    146   }
    147 }
    148 BIONIC_BENCHMARK(BM_stdlib_strtoll);
    149 
    150 void BM_stdlib_strtoul(benchmark::State& state) {
    151   while (state.KeepRunning()) {
    152     benchmark::DoNotOptimize(strtoul(" -123", nullptr, 0));
    153   }
    154 }
    155 BIONIC_BENCHMARK(BM_stdlib_strtoul);
    156 
    157 void BM_stdlib_strtoull(benchmark::State& state) {
    158   while (state.KeepRunning()) {
    159     benchmark::DoNotOptimize(strtoull(" -123", nullptr, 0));
    160   }
    161 }
    162 BIONIC_BENCHMARK(BM_stdlib_strtoull);
    163