Skip to content

Commit

Permalink
add cmake magic to download google-benchmark, implement benchmark for #…
Browse files Browse the repository at this point in the history
  • Loading branch information
ericniebler committed Dec 5, 2019
1 parent ec27fa0 commit c6ca561
Show file tree
Hide file tree
Showing 10 changed files with 211 additions and 8 deletions.
12 changes: 7 additions & 5 deletions .clang-format
Expand Up @@ -47,16 +47,18 @@
IncludeBlocks: Regroup,
IncludeCategories: [
{ Regex: '^<range/v3/detail/disable_warnings.hpp',
Priority: 7},
Priority: 8},
{ Regex: '^<range/v3/range_fwd.hpp',
Priority: 5},
{ Regex: '^<range/v3',
Priority: 6},
{ Regex: '^<range/v3',
Priority: 7},
{ Regex: '^<concepts/',
Priority: 4},
Priority: 5},
{ Regex: '^<meta/',
Priority: 3},
Priority: 4},
{ Regex: '^<std/.*>$',
Priority: 3},
{ Regex: '^<benchmark/.*>$',
Priority: 2},
{ Regex: '^<.*>$',
Priority: 1},
Expand Down
2 changes: 2 additions & 0 deletions CMakeLists.txt
Expand Up @@ -64,6 +64,8 @@ if(RANGE_V3_EXAMPLES)
endif()

if(RANGE_V3_PERF)
include(gtest)
include(gbenchmark)
add_subdirectory(perf)
endif()

Expand Down
19 changes: 19 additions & 0 deletions cmake/GoogleBenchmark.cmake.in
@@ -0,0 +1,19 @@
# Copyright Eric Niebler 2019
# Distributed under the Boost Software License, Version 1.0.
# (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)

cmake_minimum_required(VERSION 2.8.2)

project(google-benchmark-download NONE)

include(ExternalProject)
ExternalProject_Add(google-benchmark
GIT_REPOSITORY https://github.com/google/benchmark.git
GIT_TAG master
SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/google-benchmark-src"
BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/google-benchmark-build"
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
TEST_COMMAND ""
)
19 changes: 19 additions & 0 deletions cmake/GoogleTest.cmake.in
@@ -0,0 +1,19 @@
# Copyright Eric Niebler 2019
# Distributed under the Boost Software License, Version 1.0.
# (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)

cmake_minimum_required(VERSION 2.8.2)

project(google-test-download NONE)

include(ExternalProject)
ExternalProject_Add(google-test
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG master
SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/google-test-src"
BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/google-test-build"
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
TEST_COMMAND ""
)
26 changes: 26 additions & 0 deletions cmake/gbenchmark.cmake
@@ -0,0 +1,26 @@
# Download and unpack googletest at configure time
configure_file(
cmake/GoogleBenchmark.cmake.in
google-benchmark-download/CMakeLists.txt)

execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/google-benchmark-download)

if(result)
message(FATAL_ERROR "CMake step for google-benchmark failed: ${result}")
endif()

execute_process(COMMAND ${CMAKE_COMMAND} --build .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/google-benchmark-download)

if(result)
message(FATAL_ERROR "Build step for google-benchmark failed: ${result}")
endif()

# Add google-benchmark directly to our build. This defines
# the benchmark and benchmark_main targets.
add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/google-benchmark-src
${CMAKE_CURRENT_BINARY_DIR}/google-benchmark-build
EXCLUDE_FROM_ALL)
32 changes: 32 additions & 0 deletions cmake/gtest.cmake
@@ -0,0 +1,32 @@
# Copyright 2019 Eric Niebler
# Distributed under the Boost Software License, Version 1.0.
# (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)

# Download and unpack googletest at configure time
configure_file(cmake/GoogleTest.cmake.in google-test-download/CMakeLists.txt)

execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/google-test-download )

if(result)
message(FATAL_ERROR "CMake step for google-test failed: ${result}")
endif()

execute_process(COMMAND ${CMAKE_COMMAND} --build .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/google-test-download )

if(result)
message(FATAL_ERROR "Build step for google-test failed: ${result}")
endif()

# Prevent overriding the parent project's compiler/linker
# settings on Windows
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)

# Add google-test directly to our build. This defines
# the gtest and gtest_main targets.
add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/google-test-src
${CMAKE_CURRENT_BINARY_DIR}/google-test-build
EXCLUDE_FROM_ALL)
4 changes: 2 additions & 2 deletions cmake/ranges_options.cmake
Expand Up @@ -45,9 +45,9 @@ CMAKE_DEPENDENT_OPTION(RANGE_V3_EXAMPLES
"Build the Range-v3 examples and integrate with ctest"
ON "${is_standalone}" OFF)

CMAKE_DEPENDENT_OPTION(RANGE_V3_PERF
option(RANGE_V3_PERF
"Build the Range-v3 performance benchmarks"
ON "${is_standalone}" OFF)
OFF)

CMAKE_DEPENDENT_OPTION(RANGE_V3_DOCS
"Build the Range-v3 documentation"
Expand Down
3 changes: 3 additions & 0 deletions perf/CMakeLists.txt
Expand Up @@ -3,5 +3,8 @@ set(CMAKE_FOLDER "perf")
add_executable(counted_insertion_sort counted_insertion_sort.cpp)
target_link_libraries(counted_insertion_sort range-v3)

add_executable(range_conversion range_conversion.cpp)
target_link_libraries(range_conversion range-v3 benchmark_main)

add_executable(sort_patterns sort_patterns.cpp)
target_link_libraries(sort_patterns range-v3)
101 changes: 101 additions & 0 deletions perf/range_conversion.cpp
@@ -0,0 +1,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();
}
}
1 change: 0 additions & 1 deletion perf/sort_patterns.cpp
@@ -1,4 +1,3 @@
/// \file
// Range v3 library
//
// Copyright Eric Niebler 2013-present
Expand Down

0 comments on commit c6ca561

Please sign in to comment.