Skip to content
Snippets Groups Projects
Commit f6b97975 authored by vt4a2h's avatar vt4a2h
Browse files

Add simple benchmarks

parent cd259ca8
No related branches found
No related tags found
No related merge requests found
......@@ -29,7 +29,12 @@ include_directories(SYSTEM ${CMAKE_SOURCE_DIR}/../range-v3/include
find_package(Qt5Core)
add_executable(${PROJECT_NAME} "main.cpp" "FooBar.h" "../src/qrange.h" ${QRANGE_IMPL_INCLUDE})
set(INCLUDES "FooBar.h" "../src/qrange.h" ${QRANGE_IMPL_INCLUDE})
if(CMAKE_BUILD_TYPE EQUAL "RELEASE")
set(INCLUDES "${INCLUDES} benchmarks.h")
endif(CMAKE_BUILD_TYPE EQUAL "RELEASE")
add_executable(${PROJECT_NAME} "main.cpp" ${INCLUDES})
if(APPLE AND ${CMAKE_VERSION} VERSION_LESS "3.15.0" AND ${CMAKE_CXX_COMPILER_ID} MATCHES "AppleClang")
set(CMAKE_CXX20_STANDARD_COMPILE_OPTION "-std=c++2a")
......
#ifndef BENCHMARKS_H
#define BENCHMARKS_H
#ifdef NDEBUG
#include <chrono>
#include <iostream>
#include <memory>
#include <stack>
#include "qrange.h"
template <class F, class D>
void measureTime(const std::string &s, F f, D d, std::size_t times)
{
double time = 0;
for (std::size_t t = 0; t < times; ++t) {
auto data = d();
auto start = std::chrono::system_clock::now();
f(data);
auto end = std::chrono::system_clock::now();
std::chrono::duration<double, std::milli> elapsed = end - start;
time += elapsed.count();
}
std::cout << s << ": " << (time / times) << " msec" << " (run " << times << " times)" << std::endl;
}
std::shared_ptr<QObject> genTree(int depth = 22, std::size_t nodeChildrenCount = 2)
{
auto root = std::make_shared<QObject>();
std::stack<QObject*> s;
s.push(root.get());
while (depth--) {
std::stack<QObject*> layer;
while (!s.empty()) {
auto e = s.top();
s.pop();
for (std::size_t cc = 0; cc < nodeChildrenCount; ++cc) {
layer.push(new QObject(e));
}
}
s.swap(layer);
}
return root;
}
void setChildrenNamesOld(const std::shared_ptr<QObject> &obj)
{
for (auto &&c : obj->findChildren<QObject*>()) {
c->setObjectName("foo");
}
}
void setChildrenNamesNew(const std::shared_ptr<QObject> &obj)
{
for (auto &&c : obj.get() | qt::find_children(Qt::FindChildrenRecursively)) {
c->setObjectName("foo");
}
}
void setChildrenNamesNewWithCopy(const std::shared_ptr<QObject> &obj)
{
const std::vector<QObject*> d = obj.get() | qt::find_children(Qt::FindChildrenRecursively);
for (auto &&c : d) {
c->setObjectName("foo");
}
}
void runBenchmarks()
{
auto gen = []{ static auto tree = genTree(); return tree; };
measureTime("Old" , &setChildrenNamesOld , gen, 10);
measureTime("New" , &setChildrenNamesNew , gen, 10);
measureTime("New copy", &setChildrenNamesNewWithCopy, gen, 10);
}
#endif // NDEBUG
#endif // BENCHMARKS_H
......@@ -4,6 +4,10 @@
#include "FooBar.h"
#ifdef NDEBUG
#include "benchmarks.h"
#endif // NDEBUG
#include "qrange.h"
int main(int argc, char *argv[])
......@@ -49,5 +53,9 @@ int main(int argc, char *argv[])
assert(children.at(0) == l4);
}
#ifdef NDEBUG
runBenchmarks();
#endif // NDEBUG
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment