From 834c3239ff6eabf5d806d01a527d36c1f0d206d9 Mon Sep 17 00:00:00 2001 From: dmatetelki Date: Wed, 1 Jul 2015 14:50:18 +0200 Subject: [PATCH] test fixture float2 has a ctor time computed hash member for performance --- test/graph/fixture.hpp | 20 +++++++++++--------- test/graph/test_graph.hpp | 1 + test/graph/test_graph_algorithms.hpp | 6 ++---- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/test/graph/fixture.hpp b/test/graph/fixture.hpp index 3581d37..8a43fed 100644 --- a/test/graph/fixture.hpp +++ b/test/graph/fixture.hpp @@ -3,11 +3,16 @@ #include +inline std::size_t hash_f(float f) { return std::hash()(f); } +inline std::size_t hash_2f(float f1, float f2) { return hash_f(f1) ^ (hash_f(f2) << 1); } + struct float2 { - float x, y; + typedef float value_type; + value_type x, y; + std::size_t h; - inline float2() : x(0.0), y(0.0) {} - inline float2(float f1, float f2) : x(f1), y(f2) {} + constexpr float2() : x(0.0), y(0.0), h(0) {} + float2(value_type f1, value_type f2) : x(f1), y(f2), h(hash_2f(f1, f2)) {} }; inline bool operator ==(const float2& v1, const float2& v2) { return v1.x == v2.x && v1.y == v2.y; } @@ -15,16 +20,13 @@ inline float pow2(float f) { return f*f; } inline float dist(const float2& v1, const float2& v2) { return sqrt(pow2(v2.x - v1.x) + pow2(v2.y - v1.y)); } // inline float dist(const float2& v1, const float2& v2) { return abs(v2.x - v1.x) + abs(v2.y - v1.y); } + + namespace std { template <> struct hash { - std::size_t operator()(const float2& f2) const - { - std::size_t h1 = std::hash()(f2.x); - std::size_t h2 = std::hash()(f2.y); - return h1 ^ (h2 << 1); - } + std::size_t operator()(const float2& f2) const { return f2.h; } }; class distanceOf2float2s : public std::function diff --git a/test/graph/test_graph.hpp b/test/graph/test_graph.hpp index 15fe8cf..59c5048 100644 --- a/test/graph/test_graph.hpp +++ b/test/graph/test_graph.hpp @@ -555,6 +555,7 @@ TEST_CASE_METHOD(Fixture, "Graph performance", "[graph][data_structure][ SECTION("Adding edges") { const std::vector::Edge> edges = Fixture::getEdges(); Graph g(edges); + REQUIRE( numberOfEdges(g) == number_of_edges ); } SECTION("teardown") { diff --git a/test/graph/test_graph_algorithms.hpp b/test/graph/test_graph_algorithms.hpp index 3c4a257..0a66e34 100644 --- a/test/graph/test_graph_algorithms.hpp +++ b/test/graph/test_graph_algorithms.hpp @@ -99,12 +99,10 @@ TEST_CASE("Graph algorithms, small", "[graph][algorithm][dijkstra]" ) { } -TEST_CASE_METHOD(Fixture, "Graph algorithms, big graph", "[graph][algorithm][dijkstra]" ) { +TEST_CASE_METHOD(Fixture, "Graph algorithms, big graph", "[graph][algorithm][dijkstra][performance]" ) { - constexpr std::size_t number_of_rows = 100; + constexpr std::size_t number_of_rows = 300; constexpr std::size_t number_of_columns = number_of_rows; - constexpr std::size_t number_of_vertices = number_of_rows * number_of_columns; - constexpr std::size_t number_of_edges = numberOfEdges(number_of_rows, number_of_columns); Fixture::initOnce(number_of_rows, number_of_columns);