Actual millis in a C++

11,719

Solution 1

It's part of the language standard these days (some years now):

See it Live On Coliru

#include <chrono>
#include <iostream>

int main()
{
    using namespace std::chrono;

    auto epoch = high_resolution_clock::from_time_t(0);
    // ...
    auto now   = high_resolution_clock::now();

    auto mseconds = duration_cast<milliseconds>(now - epoch).count();

    std::cout << "millis: " << mseconds;
}

Solution 2

In C++ you also have clock()

#include <time.h>

...

clock_t start = clock();
... some processing ...
clock_t stop = clock();

double elapsed = double(stop - start) / CLOCKS_PER_SEC;

There are also other more accurate ways of measure timings, but they are more dependent on which king of system you're running your programs on.

Solution 3

If you have C++11 support available, you may want to look into std::chrono.

#include <iostream>
#include <chrono>
#include <ctime>

long fibonacci(int n)
{
    if (n < 3) return 1;
    return fibonacci(n-1) + fibonacci(n-2);
}

int main()
{
    std::chrono::time_point<std::chrono::system_clock> start, end;
    start = std::chrono::system_clock::now();
    std::cout << "f(42) = " << fibonacci(42) << '\n';
    end = std::chrono::system_clock::now();

    std::chrono::duration<double> elapsed_seconds = end-start;
    std::time_t end_time = std::chrono::system_clock::to_time_t(end);

    std::cout << "finished computation at " << std::ctime(&end_time)
              << "elapsed time: " << elapsed_seconds.count() << "s\n";
}

Failing that, you could use the C style std::time as follows:

#include <ctime>
#include <iostream>

int main()
{
    std::time_t result = std::time(NULL);
    std::cout << std::asctime(std::localtime(&result))
              << result << " seconds since the Epoch\n";
}
Share:
11,719

Related videos on Youtube

PEAR
Author by

PEAR

NullPointerException: "About Me" could not be found

Updated on June 04, 2022

Comments

  • PEAR
    PEAR almost 2 years

    Is it possible to get the actual millis since I-don't-know in a C++-programm like System.currentTimeMillis() in Java? I know time(), but I think it's not exactly enough to measure short times, is it?

  • PEAR
    PEAR about 10 years
    Ok, is there any constant to get milliseconds?
  • smoothware
    smoothware about 10 years
    +1 This is how I would do it in C++. Note: using namespace std::chrono; seems very worth it here.
  • David Schwartz
    David Schwartz about 10 years
    The clock function returns CPU time. I get the impression this is asking about wall time.
  • sehe
    sehe about 10 years
    What do seconds sinds the epoch have to do with things?
  • László Papp
    László Papp about 10 years
    @sehe: read about System.currentTimeMillis in the original question, please. This may be a good starting point.
  • sehe
    sehe about 10 years
    Changed the sample to actually print milliseconds since the epoch
  • sehe
    sehe about 10 years
  • 6502
    6502 about 10 years
    @pear: what about multiplying the result of the division by 1000? :-)
  • Walter
    Walter about 10 years
    @LaszloPapp This will not work without C++11 support -- there no point to consider outdated standards, unless you're dealing with legacy code.
  • László Papp
    László Papp about 10 years
    @Walter: clearly, you have not worked much with embedded. I understand bleeding edge desktop people hardly see out of their box, but that is just a specific domain. To be fair, even on desktop there are plenty of VS2010 and VS2008 users, not to mention Windows CE, etc.
  • sehe
    sehe about 10 years
    Okay, time to calm down people. It's all useful information. No need to imply incompetence left or right
  • Matthieu M.
    Matthieu M. about 10 years
    @LaszloPapp: And let's not forget corporate world, the migration from gcc 3.4 to gcc 4.3 is still ongoing where I work (has been for the last 7 years)...
  • László Papp
    László Papp about 10 years
    @MatthieuM.: yes, I agree.
  • PEAR
    PEAR about 10 years
    @6502: Of course, but this would not improve the accuracy, would it?
  • 6502
    6502 about 10 years
    @PEAR: the value of the constant CLOCKS_PER_SEC is system dependent and the same goes for the provided accuracy (they are not required to be aligned; e.g. CLOCKS_PER_SEC could be 100000 but the accuracy just 20ms).
  • Tad
    Tad over 9 years
    should the code say auto epoch = system_clock::from_time_t(0); ? My code wouldn't compile with high_resolution_clock::from_time_t(0);
  • sehe
    sehe over 9 years
    @Tad your compiler might not be to update/have full support for std::chrono (yet): en.cppreference.com/w/cpp/chrono/high_resolution_clock. Depending on the resolution you desire, system_clock could be fine for your purposes
  • Tad
    Tad over 9 years
    I used this code and time calculated was 1412002099144 miliseconds. Is time 0 in 1970? Why not use system_clock::now()? I am using gcc 4.9
  • sehe
    sehe over 9 years
    @Tad look at the question for context. Heck, here's a link for you docs.oracle.com/javase/7/docs/api/java/lang/…