tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

ostringstream_benchmark.cc (3236B)


      1 // Copyright 2018 The Abseil Authors.
      2 //
      3 // Licensed under the Apache License, Version 2.0 (the "License");
      4 // you may not use this file except in compliance with the License.
      5 // You may obtain a copy of the License at
      6 //
      7 //      https://www.apache.org/licenses/LICENSE-2.0
      8 //
      9 // Unless required by applicable law or agreed to in writing, software
     10 // distributed under the License is distributed on an "AS IS" BASIS,
     11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 // See the License for the specific language governing permissions and
     13 // limitations under the License.
     14 
     15 #include <sstream>
     16 #include <string>
     17 
     18 #include "absl/strings/internal/ostringstream.h"
     19 #include "benchmark/benchmark.h"
     20 
     21 namespace {
     22 
     23 enum StringType {
     24  kNone,
     25  kStdString,
     26 };
     27 
     28 // Benchmarks for std::ostringstream.
     29 template <StringType kOutput>
     30 void BM_StdStream(benchmark::State& state) {
     31  const int num_writes = state.range(0);
     32  const int bytes_per_write = state.range(1);
     33  const std::string payload(bytes_per_write, 'x');
     34  for (auto _ : state) {
     35    std::ostringstream strm;
     36    benchmark::DoNotOptimize(strm);
     37    for (int i = 0; i != num_writes; ++i) {
     38      strm << payload;
     39    }
     40    switch (kOutput) {
     41      case kNone: {
     42        break;
     43      }
     44      case kStdString: {
     45        std::string s = strm.str();
     46        benchmark::DoNotOptimize(s);
     47        break;
     48      }
     49    }
     50  }
     51 }
     52 
     53 // Create the stream, optionally write to it, then destroy it.
     54 BENCHMARK_TEMPLATE(BM_StdStream, kNone)
     55    ->ArgPair(0, 0)
     56    ->ArgPair(1, 16)   // 16 bytes is small enough for SSO
     57    ->ArgPair(1, 256)  // 256 bytes requires heap allocation
     58    ->ArgPair(1024, 256);
     59 // Create the stream, write to it, get std::string out, then destroy.
     60 BENCHMARK_TEMPLATE(BM_StdStream, kStdString)
     61    ->ArgPair(1, 16)   // 16 bytes is small enough for SSO
     62    ->ArgPair(1, 256)  // 256 bytes requires heap allocation
     63    ->ArgPair(1024, 256);
     64 
     65 // Benchmarks for OStringStream.
     66 template <StringType kOutput>
     67 void BM_CustomStream(benchmark::State& state) {
     68  const int num_writes = state.range(0);
     69  const int bytes_per_write = state.range(1);
     70  const std::string payload(bytes_per_write, 'x');
     71  for (auto _ : state) {
     72    std::string out;
     73    absl::strings_internal::OStringStream strm(&out);
     74    benchmark::DoNotOptimize(strm);
     75    for (int i = 0; i != num_writes; ++i) {
     76      strm << payload;
     77    }
     78    switch (kOutput) {
     79      case kNone: {
     80        break;
     81      }
     82      case kStdString: {
     83        std::string s = out;
     84        benchmark::DoNotOptimize(s);
     85        break;
     86      }
     87    }
     88  }
     89 }
     90 
     91 // Create the stream, optionally write to it, then destroy it.
     92 BENCHMARK_TEMPLATE(BM_CustomStream, kNone)
     93    ->ArgPair(0, 0)
     94    ->ArgPair(1, 16)   // 16 bytes is small enough for SSO
     95    ->ArgPair(1, 256)  // 256 bytes requires heap allocation
     96    ->ArgPair(1024, 256);
     97 // Create the stream, write to it, get std::string out, then destroy.
     98 // It's not useful in practice to extract std::string from OStringStream; we
     99 // measure it for completeness.
    100 BENCHMARK_TEMPLATE(BM_CustomStream, kStdString)
    101    ->ArgPair(1, 16)   // 16 bytes is small enough for SSO
    102    ->ArgPair(1, 256)  // 256 bytes requires heap allocation
    103    ->ArgPair(1024, 256);
    104 
    105 }  // namespace