brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.4 KiB · b265264 Raw
68 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// UNSUPPORTED: c++03, c++11, c++14, c++1710 11#include <algorithm>12#include <cstddef>13#include <deque>14#include <iterator>15#include <list>16#include <string>17#include <utility>18#include <vector>19 20#include "benchmark/benchmark.h"21#include "../../GenerateInput.h"22 23int main(int argc, char** argv) {24  auto std_shift_left = [](auto first, auto last, auto n) { return std::shift_left(first, last, n); };25 26  // Benchmark std::shift_left where we shift exactly one element, which is the worst case.27  {28    auto bm = []<class Container>(std::string name, auto shift_left) {29      benchmark::RegisterBenchmark(30          name,31          [shift_left](auto& st) {32            std::size_t const size = st.range(0);33            using ValueType        = typename Container::value_type;34            Container c;35            std::generate_n(std::back_inserter(c), size, [] { return Generate<ValueType>::random(); });36            std::size_t n = 1;37 38            // To avoid ending up with a fully moved-from range, restore the element that gets39            // overwritten by the shift after performing the shift.40            auto first_element = c.begin();41            auto last_element  = std::next(c.begin(), size - 1);42 43            for ([[maybe_unused]] auto _ : st) {44              benchmark::DoNotOptimize(c);45              benchmark::DoNotOptimize(n);46              ValueType tmp = *first_element;47              auto result   = shift_left(c.begin(), c.end(), n);48              *last_element = std::move(tmp);49              benchmark::DoNotOptimize(result);50            }51          })52          ->Arg(32)53          ->Arg(50) // non power-of-two54          ->Arg(1024)55          ->Arg(8192);56    };57    bm.operator()<std::vector<int>>("std::shift_left(vector<int>)", std_shift_left);58    bm.operator()<std::deque<int>>("std::shift_left(deque<int>)", std_shift_left);59    bm.operator()<std::list<int>>("std::shift_left(list<int>)", std_shift_left);60    // ranges::shift_left not implemented yet61  }62 63  benchmark::Initialize(&argc, argv);64  benchmark::RunSpecifiedBenchmarks();65  benchmark::Shutdown();66  return 0;67}68