0

Имеются две функции-таймера timerClock и timerQPC, способных замерять время выполнения функции, переданной им в виде шаблонного параметра. Необходимо передать их в исследующую функцию testingTimer, внутри которой они будут вызваны. Набросал код, который реализует это.

#include <iostream>
#include <functional>
#include <ctime>
#include <chrono>
#include <cmath>
#include <windows.h>

const unsigned long long arraySize = 1'000'000'000;

void testFunc(double* parr, int len) {
    for(int i = 0; i < len; ++i) {
        parr[i] = parr[len] * std::sin(parr[i]);
    }
}

template<typename Func>
double timerClock(const Func& testFunc) {
    std::clock_t c_start = std::clock();
    testFunc();
    return 1000 * (std::clock() - c_start) / CLOCKS_PER_SEC;
}

template<typename Func>
double timerQPC(const Func& testFunc) {
    LARGE_INTEGER freq,
                  c_start,
                  c_end;
    QueryPerformanceFrequency(&freq);
    double PFfreq = static_cast<double>(freq.QuadPart) / 1000;
    QueryPerformanceCounter(&c_start);
    testFunc();
    QueryPerformanceCounter(&c_end);
    return static_cast<double>(c_end.QuadPart - c_start.QuadPart) / PFfreq;
}

template<typename Func>
unsigned testingTimer(const Func& timer) {
    size_t len = 1000;
    double* arr = new double[1000000000];
    std::function<void()> funcBind = std::bind(testFunc, arr, len);
    while(!timer(funcBind)) {
        len += 1000;
    }
    return len;
}

int main(void) {
    std::cout << testingTimer(timerClock);
    return std::cout.rdstate();
}

Но компилятор выдает ошибку: *no matching function for call to 'testingTimer'

Насколько я понимаю, это из-за того, что он не способен вывести нужный тип параметра. Каким образом можно решить данную проблему?

2
  • Похоже, вам нужны декораторы
    – dIm0n
    30 авг 2020 в 19:46
  • А ещё вижу new, но не вижу delete, и size_t без std::
    – dIm0n
    30 авг 2020 в 19:49

1 ответ 1

2

Дело в том, что timerClock - это не функция, а шаблон функции. Это не одно и тоже. А вот timerClock<SomeType> - это уже функция. Именно её и нужно передавать в testingTimer. В Вашем случае, это timerClock<std::function<void()>>, потому что именно std::function<void()> будет ей передаваться. Если упростить Ваш код, то вот что получается:

#include <iostream>
#include <functional>
#include <ctime>
#include <chrono>
#include <cmath>

const unsigned long long arraySize = 1'000'000'000;

void testFunc(double* parr, int len) {
    for(int i = 0; i < len; ++i) {
        parr[i] = parr[len] * std::sin(parr[i]);
    }
}

template<typename Func>
double timerClock(const Func& testFuncArg) {
    std::clock_t c_start = std::clock();
    testFuncArg();
    return 1000 * (std::clock() - c_start) / CLOCKS_PER_SEC;
}

template<typename Func>
unsigned testingTimer(const Func& timer) {
    size_t len = 1000;
    double* arr = new double[1000000000];
    std::function<void()> funcBind = std::bind(testFunc, arr, len);
    while(!timer(funcBind)) {
        len += 1000;
    }
    return len;
}

int main(void) {
    std::cout << testingTimer(timerClock<std::function<void()>>);
    return std::cout.rdstate();
}
1
  • даже шаблон, а я и не заметил.. 30 авг 2020 в 19:02

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.