#include <xtd/xtd>
using namespace std;
class operations_timer {
public:
static void display_timer_properties() {
console::write_line(
"Operations timed using the system's high-resolution performance counter.");
else
long nanosec_per_tick = (1000L*1000L*1000L) /
frequency;
}
static void time_operations() {
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;
for (
int i = 0;
i <= num_iterations;
i++) {
long ticks_this_time = 0;
int input_num;
switch (operation) {
case 0:
try {
} catch (const system_exception&) {
input_num = 0;
}
time_per_parse.stop();
ticks_this_time = time_per_parse.elapsed_ticks();
break;
case 1:
if (!try_parse<int>("0", input_num))
input_num = 0;
time_per_parse.stop();
ticks_this_time = time_per_parse.elapsed_ticks();
break;
case 2:
try {
} catch (const system_exception&) {
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;
}
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();
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);
}
}
};
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...
static stopwatch start_new()
Initializes a new xtd::diagnostics::stopwatch instance, sets the xtd::diagnostics::stopwatch::elapsed...
stopwatch()=default
Initializes a new instance of the xtd::diagnostics::stopwatch class.
static int64_t frequency()
Gets the frequency of the timer as the number of nanoseconds per second. This field is read-only.
static bool is_high_resolution()
Indicates whether the timer is based on a high-resolution performance counter. This field is read-onl...
bool try_parse(const std::basic_string< char_t > &str, value_t &value)
Convert a type into a string.
Definition: parse.h:354
int parse< int >(const std::string &str, number_styles styles)
Convert a type into a string.
Definition: parse.h:103
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