58 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 <random>16#include <string>17#include <vector>18 19#include "benchmark/benchmark.h"20#include "../../GenerateInput.h"21 22int main(int argc, char** argv) {23 auto std_shuffle = [](auto first, auto last, auto& rng) { return std::shuffle(first, last, rng); };24 25 // {std,ranges}::shuffle(normal container)26 {27 auto bm = []<class Container>(std::string name, auto shuffle) {28 benchmark::RegisterBenchmark(29 name,30 [shuffle](auto& st) {31 std::size_t const size = st.range(0);32 using ValueType = typename Container::value_type;33 Container c;34 std::generate_n(std::back_inserter(c), size, [] { return Generate<ValueType>::random(); });35 std::mt19937 rng;36 37 for ([[maybe_unused]] auto _ : st) {38 benchmark::DoNotOptimize(c);39 shuffle(c.begin(), c.end(), rng);40 benchmark::DoNotOptimize(c);41 }42 })43 ->Arg(32)44 ->Arg(1024)45 ->Arg(8192);46 };47 bm.operator()<std::vector<int>>("std::shuffle(vector<int>)", std_shuffle);48 bm.operator()<std::deque<int>>("std::shuffle(deque<int>)", std_shuffle);49 bm.operator()<std::vector<int>>("rng::shuffle(vector<int>)", std::ranges::shuffle);50 bm.operator()<std::deque<int>>("rng::shuffle(deque<int>)", std::ranges::shuffle);51 }52 53 benchmark::Initialize(&argc, argv);54 benchmark::RunSpecifiedBenchmarks();55 benchmark::Shutdown();56 return 0;57}58