#include <xtd/collections/generic/list>
#include <xtd/diagnostics/stopwatch>
#include <xtd/console>
#include <limits>
class operations_timer {
public:
static void display_timer_properties() {
console::write_line(
"Operations timed using the system's high-resolution performance counter.");
else
auto nanosec_per_tick = (1000l * 1000l * 1000l) / frequency;
}
static void time_operations() {
constexpr auto num_iterations = 10000;
auto operation_names =
list {
"Operation: parse<int>(\"0\")",
"Operation: try_parse<int>(\"0\")",
"Operation: parse<int>(\"a\")",
"Operation: try_parse<int>(\"a\")"};
for (auto operation = 0; operation <= 3; operation++) {
[[maybe_unused]]
auto num_rollovers = 0
l;
auto min_ticks = std::numeric_limits<long>::max();
auto index_fastest = -1;
auto index_slowest = -1;
for (
auto i = 0;
i <= num_iterations;
i++) {
auto ticks_this_time = 0
l;
auto input_num = 0;
switch (operation) {
case 0:
try {
input_num = 0;
}
time_per_parse.stop();
ticks_this_time = time_per_parse.elapsed_ticks();
break;
case 1:
input_num = 0;
time_per_parse.stop();
ticks_this_time = time_per_parse.elapsed_ticks();
break;
case 2:
try {
input_num = 0;
}
time_per_parse.stop();
ticks_this_time = time_per_parse.elapsed_ticks();
break;
case 3:
input_num = 0;
time_per_parse.stop();
ticks_this_time = time_per_parse.elapsed_ticks();
break;
default:
break;
}
time_10k_operations.reset();
time_10k_operations.start();
} else {
if (max_ticks < ticks_this_time) {
max_ticks = ticks_this_time;
}
if (min_ticks > ticks_this_time) {
min_ticks = ticks_this_time;
}
num_ticks += ticks_this_time;
if (num_ticks < ticks_this_time) {
num_rollovers ++;
}
}
}
time_10k_operations.stop();
milli_sec = time_10k_operations.elapsed_milliseconds();
console::write_line(
" Slowest time: #{0}/{1} = {2} ticks", index_slowest, num_iterations, max_ticks);
console::write_line(
" Fastest time: #{0}/{1} = {2} ticks", index_fastest, num_iterations, min_ticks);
console::write_line(
" Average time: {0} ticks = {1} nanoseconds", num_ticks / num_iterations, (num_ticks * nanosec_per_tick) / num_iterations);
console::write_line(
" Total time looping through {0} operations: {1} milliseconds", num_iterations, milli_sec);
}
}
};
auto main() -> int {
operations_timer::display_timer_properties();
operations_timer::time_operations();
}
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
Definition list.hpp:79
static xtd::string read_line()
Reads the next line of characters from the standard input stream.
static void write_line()
Writes the current line terminator to the standard output stream using the specified format informati...
Provides a set of methods and properties that you can use to accurately measure elapsed time.
Definition stopwatch.hpp:36
static int64 frequency() noexcept
Gets the frequency of the timer as the number of nanoseconds per second. This field is read-only.
static stopwatch start_new() noexcept
Initializes a new xtd::diagnostics::stopwatch instance, sets the xtd::diagnostics::stopwatch::elapsed...
static bool is_high_resolution() noexcept
Indicates whether the timer is based on a high-resolution performance counter. This field is read-onl...
The exception that is thrown when a method call is invalid for the object's current state.
Definition system_exception.hpp:18
value_t parse(const std::string &str)
Convert a string into a type.
Definition parse.hpp:34
bool try_parse(const std::basic_string< char > &str, value_t &value) noexcept
Convert a string into a type.
Definition parse.hpp:416
@ l
The L key.
Definition console_key.hpp:110
@ i
The I key.
Definition console_key.hpp:104
The xtd::collections::generic namespace contains interfaces and classes that define generic collectio...
Definition comparer.hpp:16
The xtd::diagnostics namespace provides classes that allow you to interact with system processes,...
Definition assert_dialog_result.hpp:10
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8