1 #include "catch.hpp" 2 3 #include <map> 4 5 TEST_CASE( "benchmarked", "[!benchmark]" ) { 6 7 static const int size = 100; 8 9 std::vector<int> v; 10 std::map<int, int> m; 11 12 BENCHMARK( "Load up a vector" ) { 13 v = std::vector<int>(); 14 for(int i =0; i < size; ++i ) 15 v.push_back( i ); 16 } 17 REQUIRE( v.size() == size ); 18 19 BENCHMARK( "Load up a map" ) { 20 m = std::map<int, int>(); 21 for(int i =0; i < size; ++i ) 22 m.insert( { i, i+1 } ); 23 } 24 REQUIRE( m.size() == size ); 25 26 BENCHMARK( "Reserved vector" ) { 27 v = std::vector<int>(); 28 v.reserve(size); 29 for(int i =0; i < size; ++i ) 30 v.push_back( i ); 31 } 32 REQUIRE( v.size() == size ); 33 34 int array[size]; 35 BENCHMARK( "A fixed size array that should require no allocations" ) { 36 for(int i =0; i < size; ++i ) 37 array[i] = i; 38 } 39 int sum = 0; 40 for(int i =0; i < size; ++i ) 41 sum += array[i]; 42 REQUIRE( sum > size ); 43 } 44