• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // UNSUPPORTED: c++03, c++11, c++14
10 
11 #include "benchmark/benchmark.h"
12 #include "test_macros.h"
13 
14 #include <mutex>
15 #include <sstream>
16 
17 TEST_NOINLINE double istream_numbers();
18 
istream_numbers(std::locale * loc)19 double istream_numbers(std::locale* loc) {
20   const char* a[] = {"-6  69 -71  2.4882e-02 -100 101 -2.00005 5000000 -50000000",
21                      "-25 71   7 -9.3262e+01 -100 101 -2.00005 5000000 -50000000",
22                      "-14 53  46 -6.7026e-02 -100 101 -2.00005 5000000 -50000000"};
23 
24   int a1, a2, a3, a4, a5, a6, a7;
25   double f1 = 0.0, f2 = 0.0, q = 0.0;
26   for (int i = 0; i < 3; i++) {
27     std::istringstream s(a[i]);
28     if (loc)
29       s.imbue(*loc);
30     s >> a1 >> a2 >> a3 >> f1 >> a4 >> a5 >> f2 >> a6 >> a7;
31     q += (a1 + a2 + a3 + a4 + a5 + a6 + a7 + f1 + f2) / 1000000;
32   }
33   return q;
34 }
35 
36 struct LocaleSelector {
37   std::locale* imbue;
38   std::locale old;
39   static std::mutex mutex;
40 
LocaleSelectorLocaleSelector41   LocaleSelector(benchmark::State& state) {
42     std::lock_guard guard(mutex);
43     switch (state.range(0)) {
44     case 0: {
45       old   = std::locale::global(std::locale::classic());
46       imbue = nullptr;
47       break;
48     }
49     case 1: {
50       old = std::locale::global(std::locale::classic());
51       thread_local std::locale loc("en_US.UTF-8");
52       imbue = &loc;
53       break;
54     }
55     case 2: {
56       old = std::locale::global(std::locale::classic());
57       static std::locale loc("en_US.UTF-8");
58       imbue = &loc;
59       break;
60     }
61     case 3: {
62       old   = std::locale::global(std::locale("en_US.UTF-8"));
63       imbue = nullptr;
64       break;
65     }
66     }
67   }
68 
~LocaleSelectorLocaleSelector69   ~LocaleSelector() {
70     std::lock_guard guard(mutex);
71     std::locale::global(old);
72   }
73 };
74 
75 std::mutex LocaleSelector::mutex;
76 
BM_Istream_numbers(benchmark::State & state)77 static void BM_Istream_numbers(benchmark::State& state) {
78   LocaleSelector sel(state);
79   double i = 0;
80   while (state.KeepRunning())
81     benchmark::DoNotOptimize(i += istream_numbers(sel.imbue));
82 }
83 BENCHMARK(BM_Istream_numbers)->DenseRange(0, 3)->UseRealTime()->Threads(1)->ThreadPerCpu();
84 
BM_Ostream_number(benchmark::State & state)85 static void BM_Ostream_number(benchmark::State& state) {
86   LocaleSelector sel(state);
87   while (state.KeepRunning()) {
88     std::ostringstream ss;
89     if (sel.imbue)
90       ss.imbue(*sel.imbue);
91     ss << 0;
92     benchmark::DoNotOptimize(ss.str().c_str());
93   }
94 }
95 BENCHMARK(BM_Ostream_number)->DenseRange(0, 3)->UseRealTime()->Threads(1)->ThreadPerCpu();
96 
97 BENCHMARK_MAIN();
98