#include <xtd/xtd>
using namespace std;
class operations_timer {
public:
static void display_timer_properties() {
if (stopwatch::is_high_resolution())
console::write_line("Operations timed using the system's high-resolution performance counter.");
else
console::write_line("Operations timed using the standard date time.");
long frequency = stopwatch::frequency();
console::write_line(" Timer frequency in ticks per second = {0}", frequency);
long nanosec_per_tick = (1000L*1000L*1000L) / frequency;
console::write_line(" Timer is accurate within {0} nanoseconds", nanosec_per_tick);
}
static void time_operations() {
long nanosec_per_tick = (1000L*1000L*1000L) / stopwatch::frequency();
const long num_iterations = 10000;
vector operation_names = {"Operation: parse<int>(\"0\")", "Operation: try_parse<int>(\"0\")", "Operation: parse<int>(\"a\")", "Operation: try_parse<int>(\"a\")"};
for (int operation = 0; operation <= 3; operation++) {
long num_ticks = 0;
long num_rollovers = 0;
long max_ticks = 0;
long min_ticks = numeric_limits<long>::max();
int index_fastest = -1;
int index_slowest = -1;
long milli_sec = 0;
stopwatch time_10k_operations = stopwatch::start_new();
for (
int i = 0;
i <= num_iterations;
i++) {
long ticks_this_time = 0;
int input_num;
switch (operation) {
case 0:
time_per_parse = stopwatch::start_new();
try {
input_num = parse<int>("0");
input_num = 0;
}
break;
case 1:
time_per_parse = stopwatch::start_new();
if (!try_parse<int>("0", input_num))
input_num = 0;
break;
case 2:
time_per_parse = stopwatch::start_new();
try {
input_num = parse<int>("a");
input_num = 0;
}
break;
case 3:
time_per_parse = stopwatch::start_new();
input_num = 0;
break;
default:
break;
}
if (i == 0) {
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();
console::write_line();
console::write_line("{0} Summary:", operation_names[operation]);
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);
}
}
};
int main() {
operations_timer::display_timer_properties();
operations_timer::time_operations();
}
static xtd::ustring 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.h:27
void stop()
Stops measuring elapsed time for an interval.
void start()
Starts, or resumes, measuring elapsed time for an interval.
void reset()
Stops time interval measurement and resets the elapsed time to zero.
int64_t elapsed_milliseconds() const
Gets the total elapsed time measured by the current instance, in milliseconds.
int64_t elapsed_ticks() const
Gets the total elapsed time measured by the current instance, in timer ticks.
Defines the base class for predefined exceptions in the xtd namespace.
Definition system_exception.h:24
bool try_parse(const std::basic_string< char_t > &str, value_t &value)
Convert a type into a string.
Definition parse.h:354
The xtd::diagnostics namespace provides classes that allow you to interact with system processes,...
Definition assert_dialog_result.h:10
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition system_report.h:17