mercredi 2 mai 2018

C++ ScopeTimer doesn't work

I'm trying to implement a timer class which prints the time needed for a given scope. Somehow I can't get it to work properly. My code so far:

Main.cpp:

    #include "scopetimer.hpp"
    #include <cstdlib>
    #include <cmath>
    #include <string>
    #include <chrono>
    #include <iostream>
void work01()
{
    double numbers[10000];
    for (int i = 0; i < 10000; ++i)
    {
        numbers[i] = double(std::rand()) / double(RAND_MAX);
    }
    for (int n = 10000; n > 1; n = n - 1) {
        for (int i = 0; i < n - 1; i = i + 1) {
            if (numbers[i] > numbers[i + 1]) {
                double tmp = numbers[i];
                numbers[i] = numbers[i + 1];
                numbers[i + 1] = tmp;
            }
        }
    }
}

void work02()
{
    int* buf[1024];

    for (int i = 2; i < 1024; ++i)
        buf[i] = new int[i];
    for (int i = 2; i < 1024; ++i)
        delete[] buf[i];
}

// counts the number of primes in an interval
int work03(int n0, int n1)
{
    int freq = n1 - n0 + 1;
    for (int i = n0; i <= n1; ++i)
    {
        // Have fun: use the alternative iteration direction and see how fast
        // it gets!
        // for(int j = 2; j < i; ++j)
        for (int j = i - 1; j > 1; --j)
        {
            if (i%j == 0)
            {
                --freq;
                break;
            }
        }
    }
    return freq;
}

int main(int, char**)
{
    {   ScopeTimer("work01");
        work01();
    }
    {
        ScopeTimer("work02");
        work02();
    }
    {
        ScopeTimer("work03");
        work03(0, 10000);   
    }
    std::cout << std::endl << "Tests" << std::endl << std::endl;
    {
        clock_t start_(std::clock());
        work01();
        clock_t end_(std::clock());
        std::cout << "Test Timer: " << end_ - start_ << "ns" << std::endl;
    }   
    {
        clock_t start_(std::clock());
        work02();
        clock_t end_(std::clock());
        std::cout << "Test Timer: " << end_ - start_ << "ns" << std::endl;
    }
    {
        clock_t start_(std::clock());
        work03(0,10000);
        clock_t end_(std::clock());
        std::cout << "Test Timer: " << end_ - start_ << "ns" << std::endl;
    }

    system("Pause");
}


scopetimer.cpp

        #include "scopetimer.hpp"
        #include <cmath>
        #include <string>
        #include <chrono>
        #include <iostream>

    ScopeTimer::ScopeTimer(const std::string& name)
        :name_(name),
        start_(std::clock()) {
    }

    ScopeTimer::~ScopeTimer() {
        double elapsed = (double(std::clock() - start_) / double(CLOCKS_PER_SEC));
        std::cout << name_ << ": " << int(elapsed) << "ns" << std::endl;
    }

I tested the clock functions outside of ScopeTimer(), which works fine. So the only issues, as far as I can tell, is that I can't get ScopeTimer() to work. It always prints 0ns. I mostly followed the turorial: https://felix.abecassis.me/2011/09/cpp-timer-raii/

Kind regards

Aucun commentaire:

Enregistrer un commentaire