Skip to content

Commit

Permalink
Reduce reliance on std::time()
Browse files Browse the repository at this point in the history
  • Loading branch information
ckormanyos committed Nov 29, 2024
1 parent 01d44f5 commit 0d026d6
Show file tree
Hide file tree
Showing 19 changed files with 368 additions and 144 deletions.
33 changes: 23 additions & 10 deletions examples/example002_pi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@
#pragma GCC diagnostic ignored "-Wstringop-overflow"
#endif

#include <algorithm>
#include <cstdint>
#include <ctime>
#include <iostream>

// Disable heavyweight features via macros for this example.
#define WIDE_DECIMAL_DISABLE_IOSTREAM
#define WIDE_DECIMAL_DISABLE_DYNAMIC_MEMORY_ALLOCATION
Expand All @@ -27,6 +22,13 @@
#include <mcal_lcd/mcal_lcd_console.h>
#include <util/memory/util_n_slot_array_allocator.h>
#include <util/utility/util_baselexical_cast.h>
#include <test/stopwatch.h>

#include <algorithm>
#include <cstdint>
#include <iomanip>
#include <iostream>
#include <sstream>

namespace example002_pi
{
Expand Down Expand Up @@ -85,7 +87,9 @@ auto ::math::wide_decimal::example002_pi() -> bool
::math::wide_decimal::decwide_t<wide_decimal_digits10, local_limb_type, local_allocator_type>;
#endif

const auto start = std::clock();
using stopwatch_type = concurrency::stopwatch;

stopwatch_type my_stopwatch { };

#if defined(WIDE_DECIMAL_NAMESPACE)
const local_wide_decimal_type my_pi =
Expand All @@ -95,11 +99,20 @@ auto ::math::wide_decimal::example002_pi() -> bool
::math::wide_decimal::pi<wide_decimal_digits10, local_limb_type, local_allocator_type>(example002_pi_digits10_callback);
#endif

const auto stop = std::clock();
const float execution_time { stopwatch_type::elapsed_time<float>(my_stopwatch) };

std::cout << "Time example002_pi() : "
<< static_cast<float>(stop - start) / static_cast<float>(CLOCKS_PER_SEC)
<< std::endl;
{
std::stringstream strm { };

strm << "Time example002_pi() : "
<< std::fixed
<< std::setprecision(1)
<< execution_time
<< "s"
;

std::cout << strm.str() << std::endl;
}

#if defined(WIDE_DECIMAL_NAMESPACE)
const auto head_is_ok = std::equal(my_pi.crepresentation().cbegin(),
Expand Down
31 changes: 22 additions & 9 deletions examples/example002a_pi_small_limb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@
#pragma GCC diagnostic ignored "-Wstringop-overflow"
#endif

#include <cstdint>
#include <ctime>
#include <iostream>

// Disable heavyweight features via macros for this example.
#define WIDE_DECIMAL_DISABLE_IOSTREAM
#define WIDE_DECIMAL_DISABLE_DYNAMIC_MEMORY_ALLOCATION
Expand All @@ -26,6 +22,12 @@
#include <mcal_lcd/mcal_lcd_console.h>
#include <util/memory/util_n_slot_array_allocator.h>
#include <util/utility/util_baselexical_cast.h>
#include <test/stopwatch.h>

#include <cstdint>
#include <iomanip>
#include <iostream>
#include <sstream>

namespace example002a_pi
{
Expand Down Expand Up @@ -84,7 +86,9 @@ auto ::math::wide_decimal::example002a_pi_small_limb() -> bool
::math::wide_decimal::decwide_t<wide_decimal_digits10, local_limb_type, local_allocator_type>;
#endif

const auto start = std::clock();
using stopwatch_type = concurrency::stopwatch;

stopwatch_type my_stopwatch { };

#if defined(WIDE_DECIMAL_NAMESPACE)
const local_wide_decimal_type my_pi =
Expand All @@ -94,11 +98,20 @@ auto ::math::wide_decimal::example002a_pi_small_limb() -> bool
::math::wide_decimal::pi<wide_decimal_digits10, local_limb_type, local_allocator_type>(example002a_pi_small_limb_digits10_callback);
#endif

const auto stop = std::clock();
const float execution_time { stopwatch_type::elapsed_time<float>(my_stopwatch) };

{
std::stringstream strm { };

strm << "Time example002a_pi_small_limb() : "
<< std::fixed
<< std::setprecision(1)
<< execution_time
<< "s"
;

std::cout << "Time example002a_pi_small_limb() : "
<< static_cast<float>(stop - start) / static_cast<float>(CLOCKS_PER_SEC)
<< std::endl;
std::cout << strm.str() << std::endl;
}

#if defined(WIDE_DECIMAL_NAMESPACE)
const auto head_is_ok = std::equal(my_pi.crepresentation().cbegin(),
Expand Down
33 changes: 23 additions & 10 deletions examples/example002b_pi_100k.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
///////////////////////////////////////////////////////////////////
// Copyright Christopher Kormanyos 2020 - 2023. //
// Copyright Christopher Kormanyos 2020 - 2024. //
// Distributed under the Boost Software License, //
// Version 1.0. (See accompanying file LICENSE_1_0.txt //
// or copy at http://www.boost.org/LICENSE_1_0.txt) //
Expand All @@ -10,10 +10,6 @@
#pragma GCC diagnostic ignored "-Wstringop-overflow"
#endif

#include <cstdint>
#include <ctime>
#include <iostream>

// Disable heavyweight features via macros for this example.
//#define WIDE_DECIMAL_DISABLE_IOSTREAM
#define WIDE_DECIMAL_DISABLE_DYNAMIC_MEMORY_ALLOCATION
Expand All @@ -26,6 +22,12 @@
#include <mcal_lcd/mcal_lcd_console.h>
#include <util/memory/util_n_slot_array_allocator.h>
#include <util/utility/util_baselexical_cast.h>
#include <test/stopwatch.h>

#include <cstdint>
#include <iomanip>
#include <iostream>
#include <sstream>

#if defined(WIDE_DECIMAL_NAMESPACE)
auto WIDE_DECIMAL_NAMESPACE::math::wide_decimal::example002b_pi_100k() -> bool
Expand Down Expand Up @@ -63,7 +65,9 @@ auto ::math::wide_decimal::example002b_pi_100k() -> bool
::math::wide_decimal::decwide_t<wide_decimal_digits10, local_limb_type, local_allocator_type>;
#endif

const auto start = std::clock();
using stopwatch_type = concurrency::stopwatch;

stopwatch_type my_stopwatch { };

#if defined(WIDE_DECIMAL_NAMESPACE)
const local_wide_decimal_type my_pi =
Expand All @@ -73,11 +77,20 @@ auto ::math::wide_decimal::example002b_pi_100k() -> bool
::math::wide_decimal::pi<wide_decimal_digits10, local_limb_type, local_allocator_type>();
#endif

const auto stop = std::clock();
const float execution_time { stopwatch_type::elapsed_time<float>(my_stopwatch) };

{
std::stringstream strm { };

strm << "Time example002b_pi_100k() : "
<< std::fixed
<< std::setprecision(1)
<< execution_time
<< "s"
;

std::cout << "Time example002b_pi_100k() : "
<< static_cast<float>(stop - start) / static_cast<float>(CLOCKS_PER_SEC)
<< std::endl;
std::cout << strm.str() << std::endl;
}

#if defined(WIDE_DECIMAL_NAMESPACE)
const auto head_is_ok = std::equal(my_pi.crepresentation().cbegin(),
Expand Down
35 changes: 24 additions & 11 deletions examples/example002c_pi_quintic.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
///////////////////////////////////////////////////////////////////
// Copyright Christopher Kormanyos 2020 - 2023. //
// Copyright Christopher Kormanyos 2020 - 2024. //
// Distributed under the Boost Software License, //
// Version 1.0. (See accompanying file LICENSE_1_0.txt //
// or copy at http://www.boost.org/LICENSE_1_0.txt) //
Expand All @@ -10,11 +10,6 @@
#pragma GCC diagnostic ignored "-Wstringop-overflow"
#endif

#include <algorithm>
#include <cstdint>
#include <ctime>
#include <iostream>

// Disable heavyweight features via macros for this example.
#define WIDE_DECIMAL_DISABLE_IOSTREAM
#define WIDE_DECIMAL_DISABLE_DYNAMIC_MEMORY_ALLOCATION
Expand All @@ -24,6 +19,13 @@
#include <examples/example_decwide_t.h>
#include <math/constants/constants_pi_control_for_decwide_t.h>
#include <math/wide_decimal/decwide_t.h>
#include <test/stopwatch.h>

#include <algorithm>
#include <cstdint>
#include <iomanip>
#include <iostream>
#include <sstream>

namespace example002c_pi {

Expand Down Expand Up @@ -163,7 +165,9 @@ auto ::math::wide_decimal::example002c_pi_quintic() -> bool
::math::wide_decimal::decwide_t<wide_decimal_digits10>;
#endif

const auto start = std::clock();
using stopwatch_type = concurrency::stopwatch;

stopwatch_type my_stopwatch { };

#if defined(WIDE_DECIMAL_NAMESPACE)
const auto my_pi =
Expand All @@ -173,11 +177,20 @@ auto ::math::wide_decimal::example002c_pi_quintic() -> bool
example002c_pi::pi_borwein_quintic<::math::wide_decimal::decwide_t<wide_decimal_digits10>>(&std::cout);
#endif

const auto stop = std::clock();
const float execution_time { stopwatch_type::elapsed_time<float>(my_stopwatch) };

std::cout << "Time example002c_pi_quintic() : "
<< static_cast<float>(stop - start) / static_cast<float>(CLOCKS_PER_SEC)
<< std::endl;
{
std::stringstream strm { };

strm << "Time example002c_pi_quintic() : "
<< std::fixed
<< std::setprecision(1)
<< execution_time
<< "s"
;

std::cout << strm.str() << std::endl;
}

#if defined(WIDE_DECIMAL_NAMESPACE)
const auto head_is_ok = std::equal(my_pi.crepresentation().cbegin(),
Expand Down
31 changes: 22 additions & 9 deletions examples/example002d_pi_limb08.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@
#pragma GCC diagnostic ignored "-Wstringop-overflow"
#endif

#include <cstdint>
#include <ctime>
#include <iostream>

// Disable heavyweight features via macros for this example.
#define WIDE_DECIMAL_DISABLE_IOSTREAM
#define WIDE_DECIMAL_DISABLE_DYNAMIC_MEMORY_ALLOCATION
Expand All @@ -26,6 +22,12 @@
#include <mcal_lcd/mcal_lcd_console.h>
#include <util/memory/util_n_slot_array_allocator.h>
#include <util/utility/util_baselexical_cast.h>
#include <test/stopwatch.h>

#include <cstdint>
#include <iomanip>
#include <iostream>
#include <sstream>

namespace example002d_pi {

Expand Down Expand Up @@ -85,7 +87,9 @@ auto ::math::wide_decimal::example002d_pi_limb08() -> bool
::math::wide_decimal::decwide_t<wide_decimal_digits10, local_limb_type, local_allocator_type, float, std::int32_t>;
#endif

const auto start = std::clock();
using stopwatch_type = concurrency::stopwatch;

stopwatch_type my_stopwatch { };

#if defined(WIDE_DECIMAL_NAMESPACE)
const local_wide_decimal_type my_pi =
Expand All @@ -95,11 +99,20 @@ auto ::math::wide_decimal::example002d_pi_limb08() -> bool
::math::wide_decimal::pi<wide_decimal_digits10, local_limb_type, local_allocator_type, float, std::int32_t>(example002d_pi_limb8_digits10_callback);
#endif

const auto stop = std::clock();
const float execution_time { stopwatch_type::elapsed_time<float>(my_stopwatch) };

{
std::stringstream strm { };

strm << "Time example002d_pi_limb08() : "
<< std::fixed
<< std::setprecision(1)
<< execution_time
<< "s"
;

std::cout << "Time example002d_pi_limb08() : "
<< static_cast<float>(stop - start) / static_cast<float>(CLOCKS_PER_SEC)
<< std::endl;
std::cout << strm.str() << std::endl;
}

#if defined(WIDE_DECIMAL_NAMESPACE)
const auto head_is_ok = std::equal(my_pi.crepresentation().cbegin(),
Expand Down
31 changes: 23 additions & 8 deletions examples/example006_logarithm.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
///////////////////////////////////////////////////////////////////
// Copyright Christopher Kormanyos 2020 - 2022. //
// Copyright Christopher Kormanyos 2020 - 2024. //
// Distributed under the Boost Software License, //
// Version 1.0. (See accompanying file LICENSE_1_0.txt //
// or copy at http://www.boost.org/LICENSE_1_0.txt) //
///////////////////////////////////////////////////////////////////

#include <cstdint>

#include <examples/example_decwide_t.h>
#include <math/wide_decimal/decwide_t.h>
#include <test/stopwatch.h>

#include <cstdint>
#include <iomanip>
#include <iostream>
#include <sstream>

#if defined(WIDE_DECIMAL_NAMESPACE)
auto WIDE_DECIMAL_NAMESPACE::math::wide_decimal::example006_logarithm() -> bool
Expand Down Expand Up @@ -48,7 +52,9 @@ auto ::math::wide_decimal::example006_logarithm() -> bool

bool result_is_ok = true;

const auto start = std::clock();
using stopwatch_type = concurrency::stopwatch;

stopwatch_type my_stopwatch { };

for(auto i = static_cast<unsigned>(0U); i < static_cast<unsigned>(UINT32_C(1000)); ++i)
{
Expand All @@ -63,11 +69,20 @@ auto ::math::wide_decimal::example006_logarithm() -> bool
x *= 3U;
}

const auto stop = std::clock();
const float execution_time { stopwatch_type::elapsed_time<float>(my_stopwatch) };

{
std::stringstream strm { };

std::cout << "Time example006_logarithm() : "
<< static_cast<float>(stop - start) / static_cast<float>(CLOCKS_PER_SEC)
<< std::endl;
strm << "Time example006_logarithm() : "
<< std::fixed
<< std::setprecision(1)
<< execution_time
<< "s"
;

std::cout << strm.str() << std::endl;
}

return result_is_ok;
}
Expand Down
Loading

0 comments on commit 0d026d6

Please sign in to comment.