From 9c831a1a76a12427c0cc44097f4cb5744a19bbea Mon Sep 17 00:00:00 2001 From: Christophe Riccio Date: Fri, 3 May 2019 18:24:49 +0200 Subject: [PATCH] Added performance tests --- glm/ext/scalar_integer.inl | 1 + test/ext/ext_scalar_integer.cpp | 211 ++++++++++++++++++++++++++++++-- 2 files changed, 199 insertions(+), 13 deletions(-) diff --git a/glm/ext/scalar_integer.inl b/glm/ext/scalar_integer.inl index 35e457ee..2d7a121d 100644 --- a/glm/ext/scalar_integer.inl +++ b/glm/ext/scalar_integer.inl @@ -96,6 +96,7 @@ namespace detail template GLM_FUNC_QUALIFIER static genType call(genType Source, genType Multiple) { + assert(Multiple > genType(0)); if(Source > genType(0)) { genType Tmp = Source - genType(1); diff --git a/test/ext/ext_scalar_integer.cpp b/test/ext/ext_scalar_integer.cpp index b6e27dcd..a51a6d8e 100644 --- a/test/ext/ext_scalar_integer.cpp +++ b/test/ext/ext_scalar_integer.cpp @@ -4,6 +4,7 @@ #include #include #include +#include namespace isPowerOfTwo { @@ -369,6 +370,157 @@ namespace prevMultiple namespace nextMultiple { + static glm::uint const Multiples = 128; + + int perf_nextMultiple(glm::uint Samples) + { + std::vector Results(Samples * Multiples); + + std::chrono::high_resolution_clock::time_point t0 = std::chrono::high_resolution_clock::now(); + + for(glm::uint Source = 0; Source < Samples; ++Source) + for(glm::uint Multiple = 0; Multiple < Multiples; ++Multiple) + { + Results[Source * Multiples + Multiple] = glm::nextMultiple(Source, Multiples); + } + + std::chrono::high_resolution_clock::time_point t1 = std::chrono::high_resolution_clock::now(); + + std::printf("- glm::nextMultiple Time %d microseconds\n", static_cast(std::chrono::duration_cast(t1 - t0).count())); + + glm::uint Result = 0; + for(std::size_t i = 0, n = Results.size(); i < n; ++i) + Result += Results[i]; + + return Result > 0; + } + + template + GLM_FUNC_QUALIFIER T nextMultipleMod(T Source, T Multiple) + { + T const Tmp = Source - static_cast(1); + return Tmp + (Multiple - (Tmp % Multiple)); + } + + int perf_nextMultipleMod(glm::uint Samples) + { + std::vector Results(Samples * Multiples); + + std::chrono::high_resolution_clock::time_point t0 = std::chrono::high_resolution_clock::now(); + + for(glm::uint Multiple = 0; Multiple < Multiples; ++Multiple) + for (glm::uint Source = 0; Source < Samples; ++Source) + { + Results[Source * Multiples + Multiple] = nextMultipleMod(Source, Multiples); + } + + std::chrono::high_resolution_clock::time_point t1 = std::chrono::high_resolution_clock::now(); + + std::printf("- nextMultipleMod Time %d microseconds\n", static_cast(std::chrono::duration_cast(t1 - t0).count())); + + glm::uint Result = 0; + for(std::size_t i = 0, n = Results.size(); i < n; ++i) + Result += Results[i]; + + return Result > 0; + } + + template + GLM_FUNC_QUALIFIER T nextMultipleNeg(T Source, T Multiple) + { + if(Source > static_cast(0)) + { + T const Tmp = Source - static_cast(1); + return Tmp + (Multiple - (Tmp % Multiple)); + } + else + return Source + (-Source % Multiple); + } + + int perf_nextMultipleNeg(glm::uint Samples) + { + std::vector Results(Samples * Multiples); + + std::chrono::high_resolution_clock::time_point t0 = std::chrono::high_resolution_clock::now(); + + for(glm::uint Source = 0; Source < Samples; ++Source) + for(glm::uint Multiple = 0; Multiple < Multiples; ++Multiple) + { + Results[Source * Multiples + Multiple] = nextMultipleNeg(Source, Multiples); + } + + std::chrono::high_resolution_clock::time_point t1 = std::chrono::high_resolution_clock::now(); + + std::printf("- nextMultipleNeg Time %d microseconds\n", static_cast(std::chrono::duration_cast(t1 - t0).count())); + + glm::uint Result = 0; + for (std::size_t i = 0, n = Results.size(); i < n; ++i) + Result += Results[i]; + + return Result > 0; + } + + template + GLM_FUNC_QUALIFIER T nextMultipleUFloat(T Source, T Multiple) + { + return Source + (Multiple - std::fmod(Source, Multiple)); + } + + int perf_nextMultipleUFloat(glm::uint Samples) + { + std::vector Results(Samples * Multiples); + + std::chrono::high_resolution_clock::time_point t0 = std::chrono::high_resolution_clock::now(); + + for(glm::uint Source = 0; Source < Samples; ++Source) + for(glm::uint Multiple = 0; Multiple < Multiples; ++Multiple) + { + Results[Source * Multiples + Multiple] = nextMultipleUFloat(static_cast(Source), static_cast(Multiples)); + } + + std::chrono::high_resolution_clock::time_point t1 = std::chrono::high_resolution_clock::now(); + + std::printf("- nextMultipleUFloat Time %d microseconds\n", static_cast(std::chrono::duration_cast(t1 - t0).count())); + + float Result = 0; + for (std::size_t i = 0, n = Results.size(); i < n; ++i) + Result += Results[i]; + + return Result > static_cast(0); + } + + template + GLM_FUNC_QUALIFIER T nextMultipleFloat(T Source, T Multiple) + { + if(Source > static_cast(0)) + return Source + (Multiple - std::fmod(Source, Multiple)); + else + return Source + std::fmod(-Source, Multiple); + } + + int perf_nextMultipleFloat(glm::uint Samples) + { + std::vector Results(Samples * Multiples); + + std::chrono::high_resolution_clock::time_point t0 = std::chrono::high_resolution_clock::now(); + + for(glm::uint Source = 0; Source < Samples; ++Source) + for(glm::uint Multiple = 0; Multiple < Multiples; ++Multiple) + { + Results[Source * Multiples + Multiple] = nextMultipleFloat(static_cast(Source), static_cast(Multiples)); + } + + std::chrono::high_resolution_clock::time_point t1 = std::chrono::high_resolution_clock::now(); + + std::printf("- nextMultipleFloat Time %d microseconds\n", static_cast(std::chrono::duration_cast(t1 - t0).count())); + + float Result = 0; + for (std::size_t i = 0, n = Results.size(); i < n; ++i) + Result += Results[i]; + + return Result > static_cast(0); + } + template struct type { @@ -378,20 +530,49 @@ namespace nextMultiple }; template - int run() + int test_uint() { type const Data[] = { - { 8, 3, 6 }, - { 7, 7, 7 } + { 3, 4, 4 }, + { 6, 3, 6 }, + { 5, 3, 6 }, + { 7, 7, 7 }, + { 0, 1, 0 }, + { 8, 3, 9 } }; int Error = 0; for(std::size_t i = 0, n = sizeof(Data) / sizeof(type); i < n; ++i) { - T const Result = glm::nextMultiple(Data[i].Source, Data[i].Multiple); - Error += Data[i].Return == Result ? 0 : 1; + T const Result0 = glm::nextMultiple(Data[i].Source, Data[i].Multiple); + Error += Data[i].Return == Result0 ? 0 : 1; + assert(!Error); + + T const Result1 = nextMultipleMod(Data[i].Source, Data[i].Multiple); + Error += Data[i].Return == Result1 ? 0 : 1; + assert(!Error); + } + + return Error; + } + + int perf() + { + int Error = 0; + + glm::uint const Samples = 10000; + + for(int i = 0; i < 4; ++i) + { + std::printf("Run %d :\n", i); + Error += perf_nextMultiple(Samples); + Error += perf_nextMultipleMod(Samples); + Error += perf_nextMultipleNeg(Samples); + Error += perf_nextMultipleUFloat(Samples); + Error += perf_nextMultipleFloat(Samples); + std::printf("\n"); } return Error; @@ -401,15 +582,19 @@ namespace nextMultiple { int Error = 0; - Error += run(); - Error += run(); - Error += run(); - Error += run(); + Error += test_uint(); + Error += test_uint(); + Error += test_uint(); + Error += test_uint(); - Error += run(); - Error += run(); - Error += run(); - Error += run(); + Error += test_uint(); + Error += test_uint(); + Error += test_uint(); + Error += test_uint(); + +# ifdef NDEBUG + Error += perf(); +# endif//NDEBUG return Error; }