D
D
devalone2017-07-19 20:09:40
C++ / C#
devalone, 2017-07-19 20:09:40

How to properly measure code?

I wrote my bike to measure the speed of arithmetic operations https://github.com/DevAlone/CppCodeMeasurer
I measure time using std::chrono (highresolutiontimer.hpp file). I run the test function like this:

template <typename F, typename... Args>
Timer::precision CodeMeasurer::measure(F func, Args... args)
{
// делаем N повторений и записываем результат
    for (size_t i = 0; i < NUM_OF_REPETITION; i++) {
        t1.reset();
        func(args...);
        result = t1.elapsed();
        results[i] = result;
    }

// считаем среднее исключая крайние замеры
    result = 0;
    for (size_t i = NUM_OF_SKIPPED; i < NUM_OF_REPETITION - NUM_OF_SKIPPED; i++) {
        result += results[i];
    }
    result /= NUM_OF_REPETITION - NUM_OF_SKIPPED * 2;

    return result;
}

Passing a function through a pointer shouldn't affect the results.
During the first tests, I noticed that there is a difference between the first and subsequent runs of the function, probably because of the cache, so I only take into account the results "in the middle".
The function under test itself performs operations many times in a row:
void exec()
    {
        for (size_t i = 0; i < NUM_OF_REPETITION; i++) {
            val1 += val2;
            val2 -= val2;
            val1 *= val2;
        }
    }

I compile it like this:
g++ -O0 -o runIt ../main.cpp
So that a too smart compiler doesn't cut out my function with its optimizations I
run it like this:
sudo nice -n -20 ./runIt
Have I taken everything into account for the purity of the experiment?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question