Performance Statements

I know that C supports functional statements with assert(). Is there a method / library that supports performance statements in C / C ++? Are there any other languages?

Something along the lines of the following:

perf_assert_begin(ID1)
...
...
/* assert the time taken is less than 2000 ms */
perf_assert_end(ID1, interval(ID1) < 2000)
+4
source share
1 answer

Adoption can be performed via assertout <cassert>or static_assertwhich is built into the language.
So, why not take the time manually and then check the time difference in the expression assert?

#include <cassert>
#include <chrono>

#ifndef NDEBUG
auto start = std::chrono::high_resolution_clock::now();
#endif
...
#ifndef NDEBUG
assert(std::chrono::duration_cast<milliseconds>(
    std::chrono::high_resolution_clock::now() - start).count() < 2000
);
#endif

, NDEBUG. assert , NDEBUG , .

, NDEBUG start, GCC __COUNTER__, ( ) , , , .

+1

Source: https://habr.com/ru/post/1667348/


All Articles