Timer ===== .. cpp:class:: numpy::Timer numpy C++ class. Example ------- .. code-block:: cpp #include using namespace numpy; // Use Timer Timer obj; // ... operations ... Other Methods ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``double elapsed_milliseconds()`` - double - NP_BENCHMARK_SORTING.H:46 - :ref:`View ` * - ``void start()`` - void - NP_BENCHMARK_SORTING.H:42 - :ref:`View ` Code Examples ------------- The following examples are extracted from the test suite. .. _example-timer-elapsed_milliseconds-0: .. dropdown:: elapsed_milliseconds (np_test_1_all.cpp:6048) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6038 :emphasize-lines: 11 void test_timer_accuracyBenchmarkSorting() { std::cout << "========= test_timer_accuracy ======================="; Timer timer; timer.start(); // Sleep for a known duration (10ms) std::this_thread::sleep_for(std::chrono::milliseconds(10)); double elapsed = timer.elapsed_milliseconds(); // Timer should be approximately 10ms (allow 5ms tolerance for system variance) if (!(elapsed >= 3.0 && elapsed <= 105.0)) { std::string description = std::string("test_timer_accuracyBenchmarkSorting():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(elapsed >= 3.0 && elapsed <= 105.0)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // std::cout << "Timer accuracy test passed. Elapsed: " << elapsed << " ms" << std::endl; std::cout << " -> tests passed" << std::endl; .. _example-timer-start-1: .. dropdown:: start (np_test_1_all.cpp:6043) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6033 :emphasize-lines: 11 std::cout << " -> tests passed" << std::endl; } // ------ merging np_test_benchmark_sorting.cpp -- number of functions merged= 11 -------------------------------- void test_timer_accuracyBenchmarkSorting() { std::cout << "========= test_timer_accuracy ======================="; Timer timer; timer.start(); // Sleep for a known duration (10ms) std::this_thread::sleep_for(std::chrono::milliseconds(10)); double elapsed = timer.elapsed_milliseconds(); // Timer should be approximately 10ms (allow 5ms tolerance for system variance) if (!(elapsed >= 3.0 && elapsed <= 105.0)) { std::string description = std::string("test_timer_accuracyBenchmarkSorting():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(elapsed >= 3.0 && elapsed <= 105.0)"; std::cout << std::string("[FAIL] ") + description << std::endl;