Advertisement
Guest User

Untitled

a guest
Apr 9th, 2019
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.85 KB | None | 0 0
  1. /* Copyright (C) 2019 Wildfire Games.
  2.  * This file is part of 0 A.D.
  3.  *
  4.  * 0 A.D. is free software: you can redistribute it and/or modify
  5.  * it under the terms of the GNU General Public License as published by
  6.  * the Free Software Foundation, either version 2 of the License, or
  7.  * (at your option) any later version.
  8.  *
  9.  * 0 A.D. is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
  16.  */
  17.  
  18. #include "lib/self_test.h"
  19. #include "lib/rand.h"
  20.  
  21. #include <iostream>
  22. #include <random>
  23. #include <chrono>
  24. #include <boost/random/mersenne_twister.hpp>
  25. #include <boost/random/uniform_real_distribution.hpp>
  26.  
  27. class TestSoundGroup : public CxxTest::TestSuite
  28. {
  29. public:
  30.     void test_generation()
  31.     {
  32.         unsigned int samples = 1000000;
  33.         float min = 0.0f;
  34.         float max = 1000.0f;
  35.         float value = 0.f;
  36.  
  37.         for (unsigned int i = 0; i < samples; ++i)
  38.         {
  39.             value = RandFloat(min, max);
  40.             TS_ASSERT(value >= min && value <= max);
  41.             m_BoostRng.seed(i);
  42.             value = RandFloatBoost(min, max);
  43.             TS_ASSERT(value >= min && value <= max);
  44.             m_StlRng.seed(i);
  45.             value = RandFloatStl(min, max);
  46.             TS_ASSERT(value >= min && value <= max);
  47.         }
  48.     }
  49.  
  50.     void test_speed()
  51.     {
  52.         unsigned int samples = 10000000;
  53.         float min = 0.0f;
  54.         float max = 1.0f;
  55.         float value = 0.f;
  56.  
  57.         std::chrono::high_resolution_clock::time_point begin = std::chrono::high_resolution_clock::now();
  58.         for (unsigned int i = 0; i < samples; ++i)
  59.         {
  60.             value = RandFloat(min, max);
  61.         }
  62.         std::chrono::high_resolution_clock::time_point end = std::chrono::high_resolution_clock::now();
  63.         TS_ASSERT(value >= min && value <= max);
  64.         std::cout << std::endl;
  65.         float defaultSeconds = (std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin).count() / 1000000000.f);
  66.         std::cout << "RandFloat(float min, float max): " << defaultSeconds << " seconds" << std::endl;
  67.  
  68.         begin = std::chrono::high_resolution_clock::now();
  69.         for (unsigned int i = 0; i < samples; ++i)
  70.         {
  71.             m_BoostRng.seed(i);
  72.             value = RandFloatBoost(min, max);
  73.         }
  74.         end = std::chrono::high_resolution_clock::now();
  75.         float boostSeconds = (std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin).count() / 1000000000.f);
  76.         std::cout << "RandFloatBoost(float min, float max): " << boostSeconds << " seconds" << std::endl;
  77.         TS_ASSERT(value >= min && value <= max);
  78.  
  79.         begin = std::chrono::high_resolution_clock::now();
  80.         for (unsigned int i = 0; i < samples; ++i)
  81.         {
  82.             m_StlRng.seed(i);
  83.             value = RandFloatStl(min, max);
  84.         }
  85.  
  86.         end = std::chrono::high_resolution_clock::now();
  87.         float stlSeconds = (std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin).count() / 1000000000.f);
  88.  
  89.         std::cout << "RandFloatStl(float min, float max): " << (std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin).count() / 1000000000.f) << " seconds" << std::endl;
  90.         TS_ASSERT(value >= min && value <= max);
  91.         TS_ASSERT(stlSeconds < boostSeconds && boostSeconds > defaultSeconds && stlSeconds > defaultSeconds);
  92.     }
  93.  
  94. private:
  95.     static boost::random::mt19937 m_BoostRng;
  96.     static std::mt19937 m_StlRng;
  97.  
  98.     static float RandFloat(float min, float max)
  99.     {
  100.         return rand(static_cast<size_t>(min * 100.f), static_cast<size_t>(max * 100.f)) / 100.f;
  101.     }
  102.  
  103.     static float RandFloatBoost(float min, float max)
  104.     {
  105.         return boost::random::uniform_real_distribution<float>(min, max)(m_BoostRng);
  106.     }
  107.  
  108.     static float RandFloatStl(float min, float max)
  109.     {
  110.         return std::uniform_real_distribution<float>(min, max)(m_StlRng);
  111.     }
  112. };
  113.  
  114. boost::random::mt19937 TestSoundGroup::m_BoostRng;
  115. std::mt19937 TestSoundGroup::m_StlRng;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement