Commit 4f65db1d authored by Marco Bubke's avatar Marco Bubke

Add sorting benchmarking

parents
# This file is used to ignore files which are generated
# ----------------------------------------------------------------------------
!core/
*.*#
*.a
*.app
*.autosave
*.core
*.debug
*.embed.manifest
*.moc
*.o
*.obj
*.orig
*.prl
*.qm
*.rc
*.rej
*.res
*.so
*.so.*
*_pch.h.cpp
*_resource.rc
*_wrapper.bat
*_wrapper.sh
*~
.#*
.DS_Store
/.qmake.cache
/.qmake.stash
/.clang-format
Makefile*
Thumbs.db
core
moc_*.cpp
qrc_*.cpp
tags
ui_*.h
wrapper.bat
wrapper.sh
# qtcreator generated files
*.creator.user*
*.pro.user*
*.pyqtc.user*
*.qbs.user*
*.qmlproject.user*
/share/qtcreator/externaltools
/share/qtcreator/fonts/
/share/qtcreator/generic-highlighter/
/src/app/Info.plist
/src/plugins/**/*.json
/src/plugins/coreplugin/ide_version.h
/src/libs/qt-breakpad/bin
app_version.h
phony.c
# xemacs temporary files
*.flc
# Vim temporary files
.*.swp
# Visual Studio generated files
*.ib_pdb_index
*.idb
*.ilk
*.ncb
*.opensdf
*.pdb
*.sdf
*.sln
*.suo
*.vcproj
*.vcxproj
*vcproj.*.*.user
*vcxproj.*
# gcov files
*.gcda
*.gcno
*.gcov
# MinGW generated files
*.Debug
*.Release
# Python byte code
*.pyc
# translation related:
/share/qtcreator/translations/*_tr.h
/share/qtcreator/translations/qtcreator_untranslated.ts
# Qml caching files
*.jsc
*.qmlc
# Directories to ignore
# ---------------------
.moc/
.obj/
.pch/
.rcc/
.uic/
/*-debug/
/*-release/
/dist/gdb/*.gz
/dist/gdb/python/
/dist/gdb/qtcreator-*/
/dist/gdb/source/
/dist/gdb/staging/
/doc/qbs/
/doc/qtcreator/
/doc/qtcreator-dev/
/doc/pluginhowto/html/
/lib/
/lib64/
/libexec/
debug/
ipch/
release/
tmp/
# ignore both a directory as well as a symlink
/share/qtcreator/QtProject
# Binaries
# --------
*.dll
*.exe
/bin/buildoutputparser
/bin/clangbackend
/bin/cpaster
/bin/cplusplus-ast2png
/bin/cplusplus-frontend
/bin/cplusplus-keywordgen
/bin/cplusplus-mkvisitor
/bin/cplusplus-update-frontend
/bin/qbs*
/bin/qml2puppet
/bin/qmlpuppet
/bin/qtcreator
/bin/qtcreator_crash_handler
/bin/qtcreator_ctrlc_stub
/bin/qtcreator_process_stub
/bin/qtpromaker
/bin/sdktool
/share/doc/qtcreator/*.qch
/share/qtcreator/qbs/
/src/tools/examplesscanner/examplesscanner
/src/tools/qml/qmldump/qmldump
/src/tools/valgrindfake/valgrind-fake
# Tests
#------
/tests/auto/aggregation/tst_aggregation
/tests/auto/algorithm/tst_algorithm
/tests/auto/changeset/tst_changeset
/tests/auto/clangstaticanalyzer/clangstaticanalyzerlogfilereader/tst_clangstaticanalyzerlogfilereader
/tests/auto/clangstaticanalyzer/clangstaticanalyzerrunner/tst_clangstaticanalyzerrunnertest
/tests/auto/cplusplus/ast/tst_ast
/tests/auto/cplusplus/c99/tst_c99
/tests/auto/cplusplus/checksymbols/tst_checksymbols
/tests/auto/cplusplus/codeformatter/tst_codeformatter
/tests/auto/cplusplus/codegen/tst_codegen
/tests/auto/cplusplus/cppselectionchanger/tst_cppselectionchanger
/tests/auto/cplusplus/cxx11/tst_c99
/tests/auto/cplusplus/cxx11/tst_cxx11
/tests/auto/cplusplus/fileiterationorder/tst_fileiterationorder
/tests/auto/cplusplus/findusages/tst_findusages
/tests/auto/cplusplus/lexer/tst_lexer
/tests/auto/cplusplus/lookup/tst_lookup
/tests/auto/cplusplus/misc/tst_misc
/tests/auto/cplusplus/preprocessor/tst_preprocessor
/tests/auto/cplusplus/semantic/tst_semantic
/tests/auto/cplusplus/simplifytypes/tst_simplifytypes
/tests/auto/cplusplus/translationunit/tst_translationunit
/tests/auto/cplusplus/typeprettyprinter/tst_typeprettyprinter
/tests/auto/debugger/qt_tst_dumpers_*
/tests/auto/debugger/tst_disassembler
/tests/auto/debugger/tst_dumpers
/tests/auto/debugger/tst_gdb
/tests/auto/debugger/tst_namedemangler
/tests/auto/debugger/tst_offsets
/tests/auto/debugger/tst_olddumpers
/tests/auto/debugger/tst_simplifytypes
/tests/auto/debugger/tst_version
/tests/auto/diff/differ/tst_differ
/tests/auto/environment/tst_environment
/tests/auto/extensionsystem/pluginmanager/tst_pluginmanager
/tests/auto/extensionsystem/pluginspec/tst_pluginspec
/tests/auto/externaltool/tst_externaltool
/tests/auto/fakevim/tst_fakevim
/tests/auto/filesearch/tst_filesearch
/tests/auto/flamegraph/tst_flamegraph
/tests/auto/generichighlighter/highlighterengine/tst_highlighterengine
/tests/auto/generichighlighter/specificrules/tst_specificrules
/tests/auto/ioutils/tst_ioutils
/tests/auto/json/tst_json
/tests/auto/mapreduce/tst_mapreduce
/tests/auto/profilewriter/tst_profilewriter
/tests/auto/qml/codemodel/check/tst_codemodel_check
/tests/auto/qml/codemodel/dependencies/tst_dependencies
/tests/auto/qml/codemodel/importscheck/tst_qml_imports_check
/tests/auto/qml/persistenttrie/tst_trie_check
/tests/auto/qml/qmldesigner/bauhaustests/tst_bauhaus
/tests/auto/qml/qmldesigner/coretests/tst_coretests
/tests/auto/qml/qmldesigner/coretests/tst_qmldesigner_core
/tests/auto/qml/qmldesigner/propertyeditortests/tst_propertyeditor
/tests/auto/qml/qmleditor/qmlcodeformatter/tst_qmlcodeformatter
/tests/auto/qml/qmljssimplereader/tst_qmljssimplereader
/tests/auto/qml/qmlprojectmanager/fileformat/tst_fileformat
/tests/auto/qml/qrcparser/tst_qrcparser
/tests/auto/qml/reformatter/tst_reformatter
/tests/auto/qtcprocess/tst_qtcprocess
/tests/auto/runextensions/tst_runextensions
/tests/auto/sdktool/tst_sdktool
/tests/auto/timeline/timelineabstractrenderer/tst_timelineabstractrenderer
/tests/auto/timeline/timelineitemsrenderpass/tst_timelineitemsrenderpass
/tests/auto/timeline/timelinemodel/tst_timelinemodel
/tests/auto/timeline/timelinemodelaggregator/tst_timelinemodelaggregator
/tests/auto/timeline/timelinenotesmodel/tst_timelinenotesmodel
/tests/auto/timeline/timelinenotesrenderpass/tst_timelinenotesrenderpass
/tests/auto/timeline/timelineoverviewrenderer/tst_timelineoverviewrenderer
/tests/auto/timeline/timelinerenderer/tst_timelinerenderer
/tests/auto/timeline/timelinerenderpass/tst_timelinerenderpass
/tests/auto/timeline/timelinerenderstate/tst_timelinerenderstate
/tests/auto/timeline/timelineselectionrenderpass/tst_timelineselectionrenderpass
/tests/auto/timeline/timelinezoomcontrol/tst_timelinezoomcontrol
/tests/auto/treeviewfind/tst_treeviewfind
/tests/auto/utils/ansiescapecodehandler/tst_ansiescapecodehandler
/tests/auto/utils/fileutils/tst_fileutils
/tests/auto/utils/stringutils/tst_stringutils
/tests/auto/utils/templateengine/tst_templateengine
/tests/auto/utils/treemodel/tst_treemodel
/tests/auto/valgrind/callgrind/tst_callgrindparsertests
/tests/auto/valgrind/memcheck/modeldemo
/tests/auto/valgrind/memcheck/testapps/free1/free1
/tests/auto/valgrind/memcheck/testapps/free2/free2
/tests/auto/valgrind/memcheck/testapps/invalidjump/invalidjump
/tests/auto/valgrind/memcheck/testapps/leak1/leak1
/tests/auto/valgrind/memcheck/testapps/leak2/leak2
/tests/auto/valgrind/memcheck/testapps/leak3/leak3
/tests/auto/valgrind/memcheck/testapps/leak4/leak4
/tests/auto/valgrind/memcheck/testapps/overlap/overlap
/tests/auto/valgrind/memcheck/testapps/syscall/syscall
/tests/auto/valgrind/memcheck/testapps/uninit1/uninit1
/tests/auto/valgrind/memcheck/testapps/uninit2/uninit2
/tests/auto/valgrind/memcheck/testapps/uninit3/uninit3
/tests/auto/valgrind/memcheck/tst_parsertests
/tests/auto/valgrind/memcheck/tst_testrunner
/tests/manual/debugger/gui/gui
/tests/manual/debugger/helper/helper
/tests/manual/debugger/simple/libsimple_test_plugin.*dylib
/tests/manual/debugger/simple/simple_test_app
/tests/manual/fakevim/tst_fakevim
/tests/manual/pluginview/tst_plugindialog
/tests/manual/preprocessor/pp
/tests/manual/process/process
/tests/manual/proparser/testreader
/tests/manual/qml-ast2dot/qml-ast2dot
/tests/manual/shootout/shootout
/tests/manual/ssh/errorhandling/errorhandling
/tests/manual/ssh/remoteprocess/remoteprocess
/tests/manual/ssh/sftp/sftp
/tests/manual/ssh/sftpfsmodel/sftpfsmodel
/tests/manual/ssh/shell/shell
/tests/manual/ssh/tunnel/tunnel
/tests/tools/qml-ast2dot/qml-ast2dot
/tests/unit/echoserver/echo
/tests/unit/unittest/unittest
# qbs builds
/*-debug/
/*-release/
[submodule "benchmark"]
path = benchmark
url = https://github.com/google/benchmark.git
[submodule "googletest"]
path = googletest
url = https://github.com/google/googletest.git
Subproject commit aad6a5fa767529d3353bd3beb89e126c7b0868ca
GOOGLEBENCHMARK_DIR = $$PWD/benchmark
INCLUDEPATH += $$GOOGLEBENCHMARK_DIR/include
DEFINES += HAVE_STD_REGEX WITH_BENCHMARKS
SOURCES += \
$$GOOGLEBENCHMARK_DIR/src/benchmark.cc \
$$GOOGLEBENCHMARK_DIR/src/benchmark_register.cc \
$$GOOGLEBENCHMARK_DIR/src/colorprint.cc \
$$GOOGLEBENCHMARK_DIR/src/commandlineflags.cc \
$$GOOGLEBENCHMARK_DIR/src/complexity.cc \
$$GOOGLEBENCHMARK_DIR/src/console_reporter.cc \
$$GOOGLEBENCHMARK_DIR/src/counter.cc \
$$GOOGLEBENCHMARK_DIR/src/csv_reporter.cc \
$$GOOGLEBENCHMARK_DIR/src/json_reporter.cc \
$$GOOGLEBENCHMARK_DIR/src/reporter.cc \
$$GOOGLEBENCHMARK_DIR/src/sleep.cc \
$$GOOGLEBENCHMARK_DIR/src/statistics.cc \
$$GOOGLEBENCHMARK_DIR/src/string_util.cc \
$$GOOGLEBENCHMARK_DIR/src/sysinfo.cc \
$$GOOGLEBENCHMARK_DIR/src/timers.cc
win32:LIBS += -lShlwapi
QT += core network testlib
CONFIG += console c++1z testcase object_parallel_to_source
CONFIG -= app_bundle shared
include(gmock_dependency.pri)
include(benchmark_dependency.pri)
OBJECTS_DIR = $$OUT_PWD/obj # workaround for qmake bug in object_parallel_to_source
QMAKE_CXXFLAGS += -fno-omit-frame-pointer -mtune=broadwell
DEFINES += \
UNIT_TESTS
debug:DEFINES += NDEBUG
SOURCES += \
source/cppseminar-main.cpp \
source/sorting-benchmark.cpp \
source/polymorphism-benchmark.cpp
HEADERS += \
source/googletest.h
GOOGTEST_DIRECTORY = $$PWD/googletest
GTEST_INCLUDE_DIR = $$GOOGTEST_DIRECTORY/googletest
GMOCK_INCLUDE_DIR = $$GOOGTEST_DIRECTORY/googlemock
GTEST_SRC_DIR = $$GTEST_INCLUDE_DIR
GMOCK_SRC_DIR = $$GMOCK_INCLUDE_DIR
DEFINES += \
GTEST_HAS_STD_INITIALIZER_LIST_ \
GTEST_LANG_CXX11
INCLUDEPATH *= \
$$GTEST_INCLUDE_DIR \
$$GTEST_INCLUDE_DIR/include \
$$GTEST_SRC_DIR \
$$GMOCK_INCLUDE_DIR \
$$GMOCK_INCLUDE_DIR/include \
$$GMOCK_SRC_DIR
SOURCES += \
$$GMOCK_SRC_DIR/src/gmock-all.cc \
$$GTEST_SRC_DIR/src/gtest-all.cc
Subproject commit d175c8bf823e709d570772b038757fadf63bc632
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#include "googletest.h"
#include <benchmark/benchmark.h>
int main(int argc, char *argv[])
{
testing::InitGoogleTest(&argc, argv);
benchmark::Initialize(&argc, argv);
int testsHaveErrors = RUN_ALL_TESTS();
benchmark::RunSpecifiedBenchmarks();
return testsHaveErrors;
}
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#pragma once
#include <gmock/gmock.h>
#include <gmock/gmock-matchers.h>
#include <gtest/gtest.h>
/****************************************************************************
**
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#include <benchmark/benchmark.h>
#include <algorithm>
#include <set>
#include <numeric>
#include <random>
#include <unordered_set>
class RandomGenerator
{
public:
RandomGenerator(int seed)
: generator{seed}
{}
int operator()()
{
return uniform_distribution(generator);
}
private:
std::mt19937 generator;
std::uniform_int_distribution<> uniform_distribution{1, 1000000};
};
class Sort : public benchmark::Fixture
{
public:
void SetUp(benchmark::State &state) override
{
numbersSize = 1 << state.range(0);
numbers.clear();
numbers.reserve(numbersSize);
std::generate_n(std::back_inserter(numbers), numbersSize, generator);
}
void TearDown(benchmark::State &state) override
{
state.SetItemsProcessed(state.iterations() * numbersSize);
state.SetBytesProcessed(state.iterations() * sizeof(int) * numbersSize);
state.SetLabel("\tSample Size: " + std::to_string(numbersSize * sizeof(int) / 1000) + "kB");
}
RandomGenerator generator{212};
std::vector<int> numbers;
std::size_t numbersSize = 0;
};
BENCHMARK_DEFINE_F(Sort, Sort)(benchmark::State &state)
{
for (auto _ : state) {
std::sort(std::begin(numbers), std::end(numbers));
}
}
BENCHMARK_REGISTER_F(Sort, Sort)->DenseRange(13, 22);
BENCHMARK_DEFINE_F(Sort, StableSort)(benchmark::State &state)
{
for (auto _ : state) {
std::stable_sort(std::begin(numbers), std::end(numbers));
}
}
BENCHMARK_REGISTER_F(Sort, StableSort)->DenseRange(13, 22);
BENCHMARK_DEFINE_F(Sort, StableSortParitioning)(benchmark::State &state)
{
for (auto _ : state) {
std::stable_sort(std::begin(numbers),
std::end(numbers),
[] (int first, int second) {
return (first % 1024) < (second % 1024);
});
auto startPartition = std::partition_point(std::begin(numbers),
std::end(numbers),
[] (int entry) {
return (entry % 1024) < 255;
});
auto endPartition = std::partition_point(startPartition,
std::end(numbers),
[] (int entry) {
return (entry % 1024) < 256;
});
}
}
BENCHMARK_REGISTER_F(Sort, StableSortParitioning)->DenseRange(13, 22);
BENCHMARK_DEFINE_F(Sort, PartialSortFirst100Items)(benchmark::State &state)
{
for (auto _ : state) {
std::partial_sort(std::begin(numbers), std::next(std::begin(numbers), 256), std::end(numbers));
}
}
BENCHMARK_REGISTER_F(Sort, PartialSortFirst100Items)->DenseRange(13, 22);
BENCHMARK_DEFINE_F(Sort, PartialSortCopyFirst100Items)(benchmark::State &state)
{
std::vector<int> destination;
destination.resize(256);
for (auto _ : state) {
std::partial_sort_copy(std::begin(numbers),
std::end(numbers),
std::begin(destination),
std::end(destination));
}
}
BENCHMARK_REGISTER_F(Sort, PartialSortCopyFirst100Items)->DenseRange(13, 22);
BENCHMARK_DEFINE_F(Sort, PartialSortEverything)(benchmark::State &state)
{
for (auto _ : state) {
std::partial_sort(std::begin(numbers), std::end(numbers), std::end(numbers));
}
}
BENCHMARK_REGISTER_F(Sort, PartialSortEverything)->DenseRange(13, 22);
BENCHMARK_DEFINE_F(Sort, Partition)(benchmark::State &state)
{
for (auto _ : state) {
std::partition(std::begin(numbers),
std::end(numbers),
[] (int number) {
return number < (std::numeric_limits<int>::max() / 2);
});
}
}
BENCHMARK_REGISTER_F(Sort, Partition)->DenseRange(13, 22);
BENCHMARK_DEFINE_F(Sort, StablePartition)(benchmark::State &state)
{
for (auto _ : state) {
std::stable_partition(std::begin(numbers),
std::end(numbers),
[] (int number) {
return number < (std::numeric_limits<int>::max() / 2);
});
}
}
BENCHMARK_REGISTER_F(Sort, StablePartition)->DenseRange(13, 22);
BENCHMARK_DEFINE_F(Sort, NElemementAndPartialSort)(benchmark::State &state)
{
for (auto _ : state) {
auto sortRangeBegin = std::next(std::begin(numbers), numbersSize / 2);
auto sortRangeEnd = sortRangeBegin + 100;
std::nth_element(std::begin(numbers), sortRangeBegin, std::end(numbers));
std::partial_sort(sortRangeBegin + 1, sortRangeEnd, std::end(numbers));
}
}
BENCHMARK_REGISTER_F(Sort, NElemementAndPartialSort)->DenseRange(13, 22);
BENCHMARK_DEFINE_F(Sort, IsSorted)(benchmark::State &state)
{
for (auto _ : state) {
state.PauseTiming();
std::iota(std::begin(numbers), std::end(numbers), 1);
state.ResumeTiming();
std::is_sorted(std::begin(numbers), std::end(numbers));
}
}
BENCHMARK_REGISTER_F(Sort, IsSorted)->DenseRange(13, 22);
BENCHMARK_DEFINE_F(Sort, CreateSet)(benchmark::State &state)
{
for (auto _ : state) {
std::set<int> set(std::begin(numbers), std::end(numbers));
}
}
BENCHMARK_REGISTER_F(Sort, CreateSet)->Range(15, 22);
BENCHMARK_DEFINE_F(Sort, CreateUnorderedSet)(benchmark::State &state)
{
for (auto _ : state) {
std::unordered_set<int> set(std::begin(numbers), std::end(numbers));
}
}
BENCHMARK_REGISTER_F(Sort, CreateUnorderedSet)->Range(15, 22);
BENCHMARK_DEFINE_F(Sort, CreateUniqueSortedVector)(benchmark::State &state)
{
for (auto _ : state) {
std::sort(std::begin(numbers), std::end(numbers));
auto newEnd = std::unique(std::begin(numbers), std::end(numbers));
numbers.erase(newEnd, numbers.end());
}
}
BENCHMARK_REGISTER_F(Sort, CreateUniqueSortedVector)->Range(15, 22);
class Search : public Sort
{
public:
void SetUp(benchmark::State &state) override
{
Sort::SetUp(state);
samplesSize = 1 << state.range(1);
samples.clear();
samples.reserve(samplesSize);
std::generate_n(std::back_inserter(samples), samplesSize, generator);
}
void TearDown(benchmark::State &state) override
{
state.SetItemsProcessed(state.iterations() * numbersSize);
state.SetBytesProcessed(state.iterations() * sizeof(int) * numbersSize * samplesSize);
state.SetLabel("\tSamples: " + std::to_string(samplesSize)
+ " \tSample Size: " + std::to_string(numbersSize * sizeof(int) / 1000) + "kB");
}
RandomGenerator generator{2412};
std::vector<int> samples;
std::size_t samplesSize = 0;
};
BENCHMARK_DEFINE_F(Search, LinearSearch)(benchmark::State &state)
{
for (auto _ : state) {
for (int searchValue : samples) {
auto found = std::find(std::begin(numbers), std::end(numbers), searchValue);
bool contains = found != numbers.end();
benchmark::DoNotOptimize(contains);
}
}
}
BENCHMARK_REGISTER_F(Search, LinearSearch)->Ranges({{15, 22}, {8, 12}});
BENCHMARK_DEFINE_F(Search, SetSearch)(benchmark::State &state)
{
for (auto _ : state) {
std::set<int> set(std::begin(numbers), std::end(numbers));
for (int searchValue : samples) {
auto found = set.find(searchValue);
bool contains = found != set.end();
benchmark::DoNotOptimize(contains);
}
}
}
BENCHMARK_REGISTER_F(Search, SetSearch)->Ranges({{15, 22}, {8, 12}});
BENCHMARK_DEFINE_F(Search, UnorderedSetSearch)(benchmark::State &state)
{
for (auto _ : state) {
std::unordered_set<int> set(std::begin(numbers), std::end(numbers));
for (int searchValue : samples) {
auto found = set.find(searchValue);
bool contains = found != set.end();
benchmark::DoNotOptimize(contains);
}
}
}
BENCHMARK_REGISTER_F(Search, UnorderedSetSearch)->Ranges({{15, 22}, {8, 12}});
BENCHMARK_DEFINE_F(Search, SortAndSearch)(benchmark::State &state)
{
for (auto _ : state) {
std::sort(std::begin(numbers), std::end(numbers));
auto newEnd = std::unique(std::begin(numbers), std::end(numbers));
numbers.erase(newEnd, numbers.end());
for (int searchValue : samples) {
auto found = std::lower_bound(std::begin(numbers), std::end(numbers), searchValue);
bool contains = found != numbers.end() && *found == searchValue;
benchmark::DoNotOptimize(contains);
}
}
}
BENCHMARK_REGISTER_F(Search, SortAndSearch)->Ranges({{15, 22}, {8, 12}});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comm