xtd 0.2.0
Loading...
Searching...
No Matches

◆ elapsed_milliseconds()

int64 xtd::diagnostics::stopwatch::elapsed_milliseconds ( ) const
noexcept

Gets the total elapsed time measured by the current instance, in milliseconds.

Returns
A long integer representing the total number of milliseconds measured by the current instance.
Remarks
This property represents elapsed time rounded down to the nearest whole millisecond value. For higher precision measurements, use the xtd::diagnostics::stopwatch::elapsed, xtd::diagnostics::stopwatch::elapsed_ticks or xtd::diagnostics::stopwatch::elapsed_nanoseconds properties.
You can query the properties xtd::diagnostics::stopwatch::elapsed, xtd::diagnostics::stopwatch::elapsed_milliseconds, xtd::diagnostics::stopwatch::elapsed_ticks, and xtd::diagnostics::stopwatch::elapsed_nanoseconds while the xtd::diagnostics::stopwatch instance is running or stopped. The elapsed time properties steadily increase while the xtd::diagnostics::stopwatch is running; they remain constant when the instance is stopped.
By default, the elapsed time value of a xtd::diagnostics::stopwatch instance equals the total of all measured time intervals. Each call to start begins counting at the cumulative elapsed time; each call to xtd::diagnostics::stopwatch::stop ends the current interval measurement and freezes the cumulative elapsed time value. Use the xtd::diagnostics::stopwatch::reset method to clear the cumulative elapsed time in an existing xtd::diagnostics::stopwatch instance.
Examples
The following example demonstrates how to use the xtd::diagnostics::stopwatch class to determine the execution time for an application.
#include <xtd/collections/generic/list>
#include <xtd/diagnostics/stopwatch>
#include <xtd/console>
#include <limits>
using namespace xtd;
using namespace xtd::collections::generic;
using namespace xtd::diagnostics;
class operations_timer {
public:
static void display_timer_properties() {
// Display the timer frequency and resolution.
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.");
auto frequency = stopwatch::frequency();
console::write_line(" Timer frequency in ticks per second = {0}", frequency);
auto nanosec_per_tick = (1000l * 1000l * 1000l) / frequency;
console::write_line(" Timer is accurate within {0} nanoseconds", nanosec_per_tick);
}
static void time_operations() {
auto nanosec_per_tick = (1000l * 1000l * 1000l) / stopwatch::frequency();
constexpr auto num_iterations = 10000;
// Define the operation title names.
auto operation_names = list {"Operation: parse<int>(\"0\")", "Operation: try_parse<int>(\"0\")", "Operation: parse<int>(\"a\")", "Operation: try_parse<int>(\"a\")"};
// Time four different implementations for parsing
// an integer from a string.
for (auto operation = 0; operation <= 3; operation++) {
// Define variables for operation statistics.
auto num_ticks = 0l;
[[maybe_unused]] auto num_rollovers = 0l;
auto max_ticks = 0l;
auto min_ticks = std::numeric_limits<long>::max();
auto index_fastest = -1;
auto index_slowest = -1;
auto milli_sec = 0l;
auto time_10k_operations = stopwatch::start_new();
// Run the current operation 10001 times.
// The first execution time will be tossed out, since it can skew the average time.
for (auto i = 0; i <= num_iterations; i++) {
auto ticks_this_time = 0l;
auto input_num = 0;
auto time_per_parse = stopwatch {};
switch (operation) {
case 0:
// Parse a valid integer using a try-catch statement.
// Start a new stopwatch timer.
time_per_parse = stopwatch::start_new();
try {
input_num = parse<int>("0");
} catch (const system_exception&) {
input_num = 0;
}
// Stop the timer, and save the elapsed ticks for the operation.
time_per_parse.stop();
ticks_this_time = time_per_parse.elapsed_ticks();
break;
case 1:
// Parse a valid integer using the try_parse statement.
// Start a new stopwatch timer.
time_per_parse = stopwatch::start_new();
if (!try_parse<int>("0", input_num))
input_num = 0;
// Stop the timer, and save the elapsed ticks for the operation.
time_per_parse.stop();
ticks_this_time = time_per_parse.elapsed_ticks();
break;
case 2:
// Parse an invalid value using a try-catch statement.
// Start a new stopwatch timer.
time_per_parse = stopwatch::start_new();
try {
input_num = parse<int>("a");
} catch (const system_exception&) {
input_num = 0;
}
// Stop the timer, and save the elapsed ticks for the operation.
time_per_parse.stop();
ticks_this_time = time_per_parse.elapsed_ticks();
break;
case 3:
// Parse an invalid value using the try_parse statement.
// Start a new stopwatch timer.
time_per_parse = stopwatch::start_new();
if (!try_parse("a", input_num))
input_num = 0;
// Stop the timer, and save the elapsed ticks for the operation.
time_per_parse.stop();
ticks_this_time = time_per_parse.elapsed_ticks();
break;
default:
break;
}
// Skip over the time for the first operation, just in case it caused a one-time performance hit.
if (i == 0) {
time_10k_operations.reset();
time_10k_operations.start();
} else {
// Update operation statistics for iterations 1-10000.
if (max_ticks < ticks_this_time) {
index_slowest = i;
max_ticks = ticks_this_time;
}
if (min_ticks > ticks_this_time) {
index_fastest = i;
min_ticks = ticks_this_time;
}
num_ticks += ticks_this_time;
if (num_ticks < ticks_this_time) {
// Keep track of rollovers.
num_rollovers ++;
}
}
}
// Display the statistics for 10000 iterations.
time_10k_operations.stop();
milli_sec = time_10k_operations.elapsed_milliseconds();
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);
}
}
};
auto main() -> int {
operations_timer::display_timer_properties();
console::write_line("Press the Enter key to begin:");
operations_timer::time_operations();
}
// This code produces the following output :
//
// Operations timed using the system's high-resolution performance counter.
// Timer frequency in ticks per second = 1000000000
// Timer is accurate within 1 nanoseconds
//
// Press the Enter key to begin:
//
//
// Operation: parse<int>("0") Summary:
// Slowest time: #7232/10000 = 505 ticks
// Fastest time: #1278/10000 = 20 ticks
// Average time: 26 ticks = 26 nanoseconds
// Total time looping through 10000 operations: 28 milliseconds
//
// Operation: try_parse<int>("0") Summary:
// Slowest time: #2910/10000 = 312 ticks
// Fastest time: #5105/10000 = 19 ticks
// Average time: 20 ticks = 20 nanoseconds
// Total time looping through 10000 operations: 22 milliseconds
//
// Operation: parse<int>("a") Summary:
// Slowest time: #7001/10000 = 19279 ticks
// Fastest time: #3392/10000 = 4881 ticks
// Average time: 5878 ticks = 5878 nanoseconds
// Total time looping through 10000 operations: 5882 milliseconds
//
// Operation: try_parse<int>("a") Summary:
// Slowest time: #3157/10000 = 14341 ticks
// Fastest time: #20/10000 = 5809 ticks
// Average time: 6532 ticks = 6532 nanoseconds
// Total time looping through 10000 operations: 6536 milliseconds
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
Definition list.h:72
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.h:36
The exception that is thrown when a method call is invalid for the object's current state.
Definition system_exception.h:18
@ l
The L key.
@ i
The I key.
bool try_parse(const std::basic_string< char > &str, value_t &value) noexcept
Convert a string into a type.
Definition parse.h:417
The xtd::collections::generic namespace contains interfaces and classes that define generic collectio...
Definition comparer.h:15
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 xtd_about_box.h:10
Examples
lcd_label2.cpp.