### CPU Timer Constructor (C++) Source: https://www.boost.org/doc/libs/latest/libs/timer/doc/cpu_timers Constructs a `cpu_timer` object and immediately calls the `start()` method to begin measuring elapsed time. This ensures the timer is active upon instantiation. ```cpp cpu_timer() noexcept; ``` -------------------------------- ### CPU Timer Action: start() (C++) Source: https://www.boost.org/doc/libs/latest/libs/timer/doc/cpu_timers Begins or resumes the accumulation of elapsed time within the `cpu_timer`. This method records the current time and starts measuring from this point. The timer is active after this call (`!is_stopped()`). ```cpp void start() noexcept; ``` -------------------------------- ### Progress Display Usage Example (C++) Source: https://www.boost.org/doc/libs/latest/libs/timer/doc/original_timer Demonstrates how to use the progress_display class to show progress during a lengthy computation on a std::map. It initializes progress_display with the size of the map and increments it within the loop. ```cpp progress_display show_progress( big_map.size() ); for ( big_map_t::iterator itr = big_map:begin(); itr != big_map.end(); ++itr ) { // do the computation ... ++show_progress; } ``` -------------------------------- ### Using auto_cpu_timer in C++ Source: https://www.boost.org/doc/libs/latest/libs/timer/doc/cpu_timers Demonstrates the basic usage of `boost::timer::auto_cpu_timer` to measure the elapsed time of a code block. The timer automatically starts upon object creation and reports results upon destruction. This is useful for quick profiling of function or loop execution times. ```cpp #include #include int main() { boost::timer::auto_cpu_timer t; for (long i = 0; i < 100000000; ++i) std::sqrt(123.456L); // burn some time return 0; } ``` -------------------------------- ### CPU Timer Class Synopsis (C++) Source: https://www.boost.org/doc/libs/latest/libs/timer/doc/cpu_timers Defines the `cpu_timer` class, which measures wall clock and process elapsed time. It includes methods for starting, stopping, resuming, and querying the elapsed time, as well as formatting the results. It manages internal state to track time accumulation. ```cpp class cpu_timer { public: cpu_timer() noexcept; ~cpu_timer() noexcept = default; cpu_timer(const cpu_timer&) noexcept = default; cpu_timer& operator=(const cpu_timer&) noexcept = default; bool is_stopped() const noexcept; cpu_times elapsed() const noexcept; std::string format(int places, const std::string& format) const; std::string format(int places = default_places) const; void start() noexcept; void stop() noexcept; void resume() noexcept; }; ``` -------------------------------- ### CPU Timer Observer: format() (C++) Source: https://www.boost.org/doc/libs/latest/libs/timer/doc/cpu_timers Formats the current elapsed time using the `boost::timer::format` non-member function. Allows specifying the number of decimal places and a custom format string. It internally calls `elapsed()` to get the time values. ```cpp std::string format(int places, const std::string& format) const; std::string format(int places = default_places) const; ``` -------------------------------- ### CPU Timer Observer: elapsed() (C++) Source: https://www.boost.org/doc/libs/latest/libs/timer/doc/cpu_timers Retrieves the accumulated elapsed time from the `cpu_timer`. If the timer is stopped, it returns the total time recorded up to the last `stop()` call. If the timer is running, it returns the time accumulated since the last `start()` or `resume()` call. ```cpp cpu_times elapsed() const noexcept; ``` -------------------------------- ### Measure Wall-Clock Time Resolution (C++) Source: https://www.boost.org/doc/libs/latest/libs/timer/test/cpu_timer_info Measures the resolution of boost::timer::cpu_timer for wall-clock time. It repeatedly starts the timer, records the elapsed wall-clock time, and continues to measure until the wall-clock time changes, printing the difference in nanoseconds. This is useful for understanding the timer's precision for measuring real-world elapsed time. ```cpp #include #include #include // for atol() #include #include using boost::timer::nanosecond_type; using boost::timer::cpu_times; using boost::timer::cpu_timer; using boost::timer::auto_cpu_timer; using std::cout; using std::endl; int cpp_main( int argc, char * argv[] ) { cpu_times start_time; start_time.clear(); cpu_times current_time; // ... previous code ... { cpu_timer cpu; cout << "measure boost::timer::cpu_timer resolution for wall-clock time..." << std::endl; for (int i = 0; i < 100; ++i) { cpu.start(); start_time.wall = cpu.elapsed().wall; current_time.wall = start_time.wall; while (current_time.wall == start_time.wall) { current_time.wall = cpu.elapsed().wall; } cout << current_time.wall - start_time.wall << "ns "; } } return 0; } ``` -------------------------------- ### Measure CPU Timer Resolution (C++) Source: https://www.boost.org/doc/libs/latest/libs/timer/test/cpu_timer_info Measures the resolution of boost::timer::cpu_timer for user CPU time. It repeatedly starts the timer, records the elapsed user time, and continues to measure until the user time changes, printing the difference in nanoseconds. This helps determine the timer's precision for CPU-bound operations. ```cpp #include #include #include // for atol() #include #include using boost::timer::nanosecond_type; using boost::timer::cpu_times; using boost::timer::cpu_timer; using boost::timer::auto_cpu_timer; using std::cout; using std::endl; int cpp_main( int argc, char * argv[] ) { cpu_times start_time; start_time.clear(); cpu_times current_time; { cpu_timer cpu; cout << "measure boost::timer::cpu_timer resolution for user time..." << std::endl; for (int i = 0; i < 3; ++i) { cpu.start(); start_time = cpu.elapsed(); current_time.user = start_time.user; while (current_time.user == start_time.user) { current_time = cpu.elapsed(); } cout << current_time.user - start_time.user << "ns\n"; } } // ... rest of the code ... return 0; } ``` -------------------------------- ### Boost Timer Library Synopsis Source: https://www.boost.org/doc/libs/latest/libs/timer/doc/cpu_timers This is the synopsis for the Boost Timer library, outlining the main classes and types available. It includes `cpu_timer` for wall clock, user, and system timings, and `auto_cpu_timer` for automatic reporting on destruction. It also defines `nanosecond_type` and the `cpu_times` struct. ```c++ namespace boost { namespace timer { class cpu_timer; // wall clock, user, and system timer class auto_cpu_timer; // automatic report() on destruction typedef boost::int_least64_t nanosecond_type; struct cpu_times { nanosecond_type wall; nanosecond_type user; nanosecond_type system; void clear(); }; const int default_places = 6; std::string format(const cpu_times& times, short places, const std::string& format); std::string format(const cpu_times& times, short places = default_places); } // namespace timer } // namespace boost ``` -------------------------------- ### Progress Display Class Synopsis (C++) Source: https://www.boost.org/doc/libs/latest/libs/timer/doc/original_timer Provides the C++ synopsis for the boost::progress_display class. It outlines the constructors, member functions like restart, operator+=, operator++, count, and expected_count, along with their effects and postconditions. ```cpp #include namespace boost { class progress_display : noncopyable { public: progress_display( unsigned long expected_count ); // Effects: restart(expected_count) progress_display( unsigned long expected_count, std::ostream& os, // os is hint; implementation may ignore const std::string & s1 = "\n", //leading strings const std::string & s2 = "", const std::string & s3 = "" ) // Effects: save copy of leading strings, restart(expected_count) void restart( unsigned long expected_count ); // Effects: display appropriate scale on three lines, // prefaced by stored copy of s1, s2, s3, respectively, from constructor // Postconditions: count()==0, expected_count()==expected_count unsigned long operator+=( unsigned long increment ) // Effects: Display appropriate progress tic if needed. // Postconditions: count()== original count() + increment // Returns: count(). unsigned long operator++() // Returns: operator+=( 1 ). unsigned long count() const // Returns: The internal count. unsigned long expected_count() const // Returns: The expected_count from the constructor. }; } // namespace boost ``` -------------------------------- ### auto_cpu_timer Class Synopsis Source: https://www.boost.org/doc/libs/latest/libs/timer/doc/cpu_timers This is the synopsis for the auto_cpu_timer class, which inherits from cpu_timer. It outlines the available constructors, destructor, observers, and actions. The constructors allow customization of output streams, precision (places), and format strings. ```cpp class auto_cpu_timer : public cpu_timer { public: explicit auto_cpu_timer(short places = default_places); auto_cpu_timer(short places, const std::string& format); explicit auto_cpu_timer(const std::string& format); auto_cpu_timer(std::ostream& os, short places, const std::string& format); explicit auto_cpu_timer(std::ostream& os, short places = default_places); auto_cpu_timer(std::ostream& os, const std::string& format); ~auto_cpu_timer() noexcept; // compiler generated; shown for exposition only auto_cpu_timer(const auto_cpu_timer&) = default; auto_cpu_timer& operator=(const auto_cpu_timer&) = default; // observers std::ostream& ostream() const noexcept; short places() const noexcept; const std::string& format_string() const noexcept; // actions void report(); }; ``` -------------------------------- ### auto_cpu_timer Constructors Source: https://www.boost.org/doc/libs/latest/libs/timer/doc/cpu_timers These are the various constructors for the auto_cpu_timer class. They allow initialization with different combinations of precision (places), format strings, and output streams. If no ostream is specified, std::cout is used by default. ```cpp explicit auto_cpu_timer(short places = default_places); auto_cpu_timer(short places, const std::string& format); explicit auto_cpu_timer(const std::string& format); auto_cpu_timer(std::ostream& os, short places, const std::string& format); explicit auto_cpu_timer(std::ostream& os, short places = default_places); auto_cpu_timer(std::ostream& os, const std::string& format); ``` -------------------------------- ### Customizing auto_cpu_timer Output in C++ Source: https://www.boost.org/doc/libs/latest/libs/timer/doc/cpu_timers Illustrates how to customize the output format and precision of `boost::timer::auto_cpu_timer` using constructor arguments. This allows control over the displayed information, such as units, number of decimal places, and the overall reporting string. ```cpp boost::timer::auto_cpu_timer t; // Default output: 5.713010s wall, 5.709637s user + 0.000000s system = 5.709637s CPU (99.9%) boost::timer::auto_cpu_timer t(std::cerr, 2); // Output with 2 decimal places: 5.71s wall, 5.70s user + 0.00s system = 5.70s CPU (99.9%) boost::timer::auto_cpu_timer t(1); // Output with 1 decimal place: 5.7s wall, 5.7s user + 0.0s system = 5.7s CPU (99.9%) boost::timer::auto_cpu_timer t(3, "%w seconds\n"); // Custom format: 5.713 seconds boost::timer::auto_cpu_timer t("%t sec CPU, %w sec real"); // Custom format: 5.709637 sec CPU, 5.713010 sec real ``` -------------------------------- ### auto_cpu_timer format_string Observer Source: https://www.boost.org/doc/libs/latest/libs/timer/doc/cpu_timers This observer function returns the format string used for reporting by the auto_cpu_timer. This format string is specified during construction or copy assignment. ```cpp const std::string& format_string() const noexcept; ``` -------------------------------- ### Format CPU Times to String (C++) Source: https://www.boost.org/doc/libs/latest/libs/timer/doc/cpu_timers Converts cpu_times values to strings representing seconds, formatted according to a specified format string. Supports default formatting and custom formats with placeholders for wall, user, system, total, and percentage times. Depends on the `cpu_times` struct. ```cpp std::string format(const cpu_times& times, short places, const std::string& format); std::string format(const cpu_times& times, short places = default_places); ``` -------------------------------- ### Using cpu_timer for Checkpoints in C++ Source: https://www.boost.org/doc/libs/latest/libs/timer/doc/cpu_timers Shows how to use `boost::timer::cpu_timer` to manually track elapsed time and create checkpoints. This approach is suitable for scenarios where timing needs to be sampled at specific intervals within a longer process, rather than just at the end of a scope. ```cpp using boost::timer::cpu_timer; using boost::timer::cpu_times; using boost::timer::nanosecond_type; ... nanosecond_type const twenty_seconds(20 * 1000000000LL); nanosecond_type last(0); cpu_timer timer; while (more_transactions) { process_a_transaction(); cpu_times const elapsed_times(timer.elapsed()); nanosecond_type const elapsed(elapsed_times.system + elapsed_times.user); if (elapsed >= twenty_seconds) { ... create a checkpoint ... last = elapsed; } } ``` -------------------------------- ### Boost Progress Timer Class Synopsis Source: https://www.boost.org/doc/libs/latest/libs/timer/doc/original_timer Defines the interface for the `boost::progress_timer` class, which inherits from `boost::timer` and automatically displays elapsed time upon destruction. It can be configured to use a specific output stream. ```cpp #include namespace boost { class progress_timer : public timer, noncopyable { public: progress_timer(); progress_timer( std::ostream& os ); // os is hint; implementation may ignore ~progress_timer(); }; } // namespace boost ``` -------------------------------- ### Basic Usage of progress_timer Source: https://www.boost.org/doc/libs/latest/libs/timer/doc/original_timer Demonstrates the simple usage of `boost::progress_timer` to measure the execution time of a block of code. An elapsed time message is automatically displayed when the `progress_timer` object goes out of scope. ```cpp #include int main() { progress_timer t; // start timing // do something ... return 0; } ``` -------------------------------- ### auto_cpu_timer places Observer Source: https://www.boost.org/doc/libs/latest/libs/timer/doc/cpu_timers This observer function returns the precision (number of decimal places) setting for the auto_cpu_timer. This value is set during construction or copy assignment. ```cpp short places() const noexcept; ``` -------------------------------- ### auto_cpu_timer ostream Observer Source: https://www.boost.org/doc/libs/latest/libs/timer/doc/cpu_timers This observer function returns the output stream associated with the auto_cpu_timer object. This stream is determined during construction or via copy assignment. ```cpp std::ostream& ostream() const noexcept; ``` -------------------------------- ### auto_cpu_timer report Action Source: https://www.boost.org/doc/libs/latest/libs/timer/doc/cpu_timers This action explicitly triggers the reporting of the elapsed CPU time. It formats the time using the specified precision and format string, and outputs it to the associated ostream. It's recommended to call stop() before report() for accurate results, and resume() can be called afterwards. ```cpp void report(); ``` -------------------------------- ### CPU Timer Action: resume() (C++) Source: https://www.boost.org/doc/libs/latest/libs/timer/doc/cpu_timers Resumes accumulating elapsed time if the timer is in a stopped state. This method allows for continuous measurement across multiple start/stop intervals. If the timer is already running, this call has no effect. ```cpp void resume() noexcept; ``` -------------------------------- ### Boost Timer Class Synopsis Source: https://www.boost.org/doc/libs/latest/libs/timer/doc/original_timer Defines the interface for the `boost::timer` class, used for measuring elapsed time. It relies on the C Standard Library's `clock()` function, offering moderate portability but limited precision and a potentially short maximum measurable time. ```cpp #include namespace boost { class timer { public: timer(); // postcondition: elapsed()==0 // compiler generated copy constructor, copy assignment, and dtor apply void restart(); // post: elapsed()==0 double elapsed() const; // return elapsed time in seconds double elapsed_max() const; // return estimated maximum value for elapsed() // Portability warning: elapsed_max() may return too high a value on systems // where std::clock_t overflows or resets at surprising values. double elapsed_min() const; // return minimum value for elapsed() }; // timer } // namespace boost ``` -------------------------------- ### cpu_times Struct - Clear Function Source: https://www.boost.org/doc/libs/latest/libs/timer/doc/cpu_timers The `clear()` function within the `cpu_times` struct resets all time components (wall, user, system) to zero. This is useful for re-initializing time measurements. ```c++ void clear(); // Effects: wall = user = system = 0LL. ``` -------------------------------- ### auto_cpu_timer Destructor Source: https://www.boost.org/doc/libs/latest/libs/timer/doc/cpu_timers The destructor for auto_cpu_timer. If the timer is running when the object goes out of scope, it will automatically stop the timer and report the elapsed CPU time. It is marked noexcept, ensuring no exceptions are thrown. ```cpp ~auto_cpu_timer() noexcept; ``` -------------------------------- ### CPU Timer Action: stop() (C++) Source: https://www.boost.org/doc/libs/latest/libs/timer/doc/cpu_timers Stops the accumulation of elapsed time if the timer is currently running. This action records the elapsed time up to the point of the call, making it observable via `elapsed()`. After this call, the timer is in a stopped state (`is_stopped()`). ```cpp void stop() noexcept; ``` -------------------------------- ### CPU Timer Observer: is_stopped() (C++) Source: https://www.boost.org/doc/libs/latest/libs/timer/doc/cpu_timers Checks the current state of the `cpu_timer`. Returns `true` if the timer has been stopped by a call to `stop()`, indicating that further time accumulation has ceased. Otherwise, returns `false`. ```cpp bool is_stopped() const noexcept; ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.