FCF 2.0 development in progress...
> > > > > >
[News] [C++ Libraries API] [C++ Downloads] [Donate to the project] [Contacts]

operator()() method from fcf::NTest::DurationBasic class

void operator()(TFunction a_function)
void operator()(int a_beginLevel, TFunction a_function)
void operator()(int a_beginLevel, int a_endLevel, TFunction a_function)
void operator()(const fcf::NTest::DurationBasic::Options& a_options, TFunction a_function)
void operator()(const fcf::NTest::DurationBasic::Options& a_options, int a_beginLevel, TFunction a_function)
void operator()(const fcf::NTest::DurationBasic::Options& a_options, int a_beginLevel, int a_endLevel, TFunction a_function)

Class: fcf::NTest::DurationBasic

Package: fcfTest

File: test.hpp

Available from version: 1.2.5

Executes a function and measures its duration.

The operator() is a convenience method for performing micro-benchmarking. It executes the provided callable multiple times, handles warmup iterations, and automatically records the results into the internal histogram.

This method is highly overloaded to support various measurement configurations.

Arguments

fcf::NTest::DurationBasic::Options a_options
- Configuration options for the measurement.

int a_beginLevel
- Starting level for recording.

int a_endLevel
- Ending level for recording.

TFunction a_function
- The callable object to be benchmarked.

Example: Benchmarking a Lambda

The simplest way to benchmark a piece of code using a lambda expression.

#define FCF_TEST_IMPLEMENTATION #include <fcfTest/test.hpp> #include <vector> #include <algorithm> FCF_TEST_DEFINE("Bench", "Lambda", "SortTest") { fcf::NTest::Duration bench(1000); bench([](){ std::vector<int> v = {5, 2, 9, 1, 5, 6}; std::sort(v.begin(), v.end()); }); fcf::NTest::log() << "Benchmark completed." << std::endl; fcf::NTest::log() << " Duration: " << bench.duration().str(true) << std::endl; fcf::NTest::log() << " Average: " << bench.average().str(true) << std::endl; } int main(int a_argc, char* a_argv[]) { bool error = false; fcf::NTest::cmdRun(a_argc, a_argv, fcf::NTest::CRM_RUN, &error); return error ? 1 : 0; }

Output:

Performing the test: "Bench" -> "Lambda" -> "SortTest" ... > Benchmark completed. > Duration: 0.000`707`227 > Average: 0.000`000`707 [SUCCESS] Test completed successfully (0.000`737`940 sec) [SUCCESS] All tests were completed. Tests: 1 passed, 0 failed, 0 skipped, 1 total Duration: 0.000`737`940 sec