File: range_conversion.cpp

package info (click to toggle)
range-v3 0.12.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,652 kB
  • sloc: cpp: 76,839; xml: 226; sh: 89; python: 34; makefile: 19; perl: 15
file content (101 lines) | stat: -rw-r--r-- 2,786 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Range v3 library
//
//  Copyright 2019-present Christopher Di Bella
//  Copyright 2019-present Eric Niebler
//
//  Use, modification and distribution is subject to the
//  Boost Software License, Version 1.0. (See accompanying
//  file LICENSE_1_0.txt or copy at
//  http://www.boost.org/LICENSE_1_0.txt)
//
// Project home: https://github.com/ericniebler/range-v3
//

// Benchmark for https://github.com/ericniebler/range-v3/issues/1337

#include <string>
#include <vector>

#include <benchmark/benchmark.h>

#include <range/v3/algorithm/equal.hpp>
#include <range/v3/range/conversion.hpp>
#include <range/v3/range/primitives.hpp>
#include <range/v3/view/common.hpp>
#include <range/v3/view/reverse.hpp>
#include <range/v3/view/transform.hpp>

using namespace ranges;

namespace
{
    auto palindrome_range_common(std::vector<std::string> const & words)
    {
        auto is_palindrome = [](auto const & word) {
            return !ranges::empty(word) && ranges::equal(word, word | views::reverse);
        };

        auto palindrome_excalim = [&is_palindrome](auto const & word) {
            return is_palindrome(word) ? word + '!' : word;
        };

        auto result = words | views::transform(palindrome_excalim) | views::common;

        return std::vector<std::string>{ranges::begin(result), ranges::end(result)};
    }

    auto palindrome_range_to(std::vector<std::string> const & words)
    {
        auto is_palindrome = [](auto const & word) {
            return !ranges::empty(word) && ranges::equal(word, word | views::reverse);
        };

        auto palindrome_excalim = [&is_palindrome](auto const & word) {
            return is_palindrome(word) ? word + '!' : word;
        };

        return words | views::transform(palindrome_excalim) | ranges::to<std::vector>;
    }
} // namespace

class Words : public ::benchmark::Fixture
{
protected:
    std::vector<std::string> words_;

public:
    void SetUp(const ::benchmark::State &)
    {
        auto magic = 476'000u;
        words_.reserve(magic);
        for(auto i = 0u; i < magic; ++i)
        {
            words_.push_back("this");
            words_.push_back("is");
            words_.push_back("his");
            words_.push_back("face");
            words_.push_back("abba");
            words_.push_back("toot");
        }
    }
};

BENCHMARK_F(Words, RangeCommon)(benchmark::State & st)
{
    for(auto _ : st)
    {
        auto result = ::palindrome_range_common(words_);
        benchmark::DoNotOptimize(result.data());
        benchmark::ClobberMemory();
    }
}

BENCHMARK_F(Words, RangeTo)(benchmark::State & st)
{
    for(auto _ : st)
    {
        auto result = ::palindrome_range_to(words_);
        benchmark::DoNotOptimize(result.data());
        benchmark::ClobberMemory();
    }
}