0

I´m trying to do something that I thought would be very simple but I have looked everywhere and I can´t figure it out. I´m also new to C++ and have no good understanding of templates and such.

I just need a function that measures the time from the program´s launch to a certain point in milliseconds, something like:

class timeCounter {

    private:
        long startTime;
        long currentTime;
        long timeDifference;
    public:
        long getTime();
}

timeCounter::timeCounter () {
    startTime = time.now();
}

long timeCounter::getTimePassed () {
    currentTime = time.now();
    timeDifference = timeNow - timeStart;
    return timeDifference;
}

I´ve tried with clock() / CLOCKS_PER_SECONDS but the result is slower than a second.

Can anyone help me out?

Thank you very much!

Tomaz Fernandes
  • 2,429
  • 2
  • 14
  • 18
  • Try looking here http://stackoverflow.com/questions/728068/how-to-calculate-a-time-difference-in-c http://stackoverflow.com/questions/483164/looking-for-benchmarking-code-snippet-c – Bauss Jul 18 '15 at 04:48
  • I´ve tried that, but for some reason when I divide clock() by CLOCKS_PER_SECOND the result is slower than a second... – Tomaz Fernandes Jul 18 '15 at 04:50
  • That's not surprising when you carefully read the documentation for `clock()`. In particular, compare it to the definition of `time()`. – Ulrich Eckhardt Jul 18 '15 at 04:55
  • well.. what is your `time` object? where is this from? – Russell Greene Jul 18 '15 at 04:59
  • http://stackoverflow.com/questions/10455905/clocks-per-sec-not-actually-clocks-per-sec – dlask Jul 18 '15 at 05:00
  • I´ve looked up here: [http://en.cppreference.com/w/cpp/chrono/c/clock] and got why clock() doesn´t work. But in here [http://www.cplusplus.com/reference/ctime/time/] it seems like it only measures in seconds, and I need it in milliseconds... – Tomaz Fernandes Jul 18 '15 at 05:02
  • This `time` object is only a wishful object for what I´m looking for =c) – Tomaz Fernandes Jul 18 '15 at 05:04
  • Actually I couldn´t find the documentation for time(), which library does it belong to? – Tomaz Fernandes Jul 18 '15 at 05:09
  • Is knowing the time elapsed since load-inception the *only* reason you're not using [`std::chrono::high_resolution_clock`](http://en.cppreference.com/w/cpp/chrono/high_resolution_clock) and [`std::chrono::duration_cast`](http://en.cppreference.com/w/cpp/chrono/duration/duration_cast) segmented by [`std::chrono::milliseconds`](http://en.cppreference.com/w/cpp/chrono/duration) ? Depending on your *real* needs, I would think the sample provided for `duration_cast` would be a decent fit for what you seek. – WhozCraig Jul 18 '15 at 05:22
  • I´m really trying to figure out how to use `` but I can´t yet understand how to use these templates, I´m really new to C++. I just need a time reference from the instantiation of a class to a given point in time so I can build a musical metronome object... – Tomaz Fernandes Jul 18 '15 at 05:29

1 Answers1

3

I was recently writing a similar system to get the delta time for a game engine.

Using the std::chrono library, here's an example:

#include <iostream>
#include <chrono>
#include <thread>

class timer
{
    // alias our types for simplicity
    using clock             = std::chrono::system_clock;
    using time_point_type   = std::chrono::time_point < clock, std::chrono::milliseconds > ;
public:
    // default constructor that stores the start time
    timer()
    {
        start = std::chrono::time_point_cast<std::chrono::milliseconds>(clock::now());
    }

    // gets the time elapsed from construction.
    long /*milliseconds*/ getTimePassed()
    {
        // get the new time
        auto end = clock::now();

        // return the difference of the times
        return (end - start).count();
    }

private:
    time_point_type start;
};

int main()
{
    timer t;

    std::this_thread::sleep_for(std::chrono::seconds(5));

    std::cout << t.getTimePassed();

    std::cin.get();
}
Russell Greene
  • 2,141
  • 17
  • 29