5

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?

László Papp
  • 51,870
  • 39
  • 111
  • 135
PEAR
  • 685
  • 3
  • 10
  • 20
  • Take a look at these questions: [how to get time in millis in c++ just like java](http://stackoverflow.com/questions/2831841/how-to-get-time-in-millis-in-c-just-like-java) and [C++ Timer function to provide time in nano seconds](http://stackoverflow.com/questions/275004/c-timer-function-to-provide-time-in-nano-seconds) – manlio Apr 05 '14 at 10:39
  • @slugonamission: that is *nix specific... – László Papp Apr 05 '14 at 10:41
  • @LaszloPapp - oops, deleted. – slugonamission Apr 05 '14 at 10:44

3 Answers3

3

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.

6502
  • 112,025
  • 15
  • 165
  • 265
  • Ok, is there any constant to get milliseconds? – PEAR Apr 05 '14 at 10:40
  • 3
    The `clock` function returns CPU time. I get the impression this is asking about wall time. – David Schwartz Apr 05 '14 at 10:44
  • 1
    @pear: what about multiplying the result of the division by 1000? :-) – 6502 Apr 05 '14 at 11:49
  • @6502: Of course, but this would not improve the accuracy, would it? – PEAR Apr 06 '14 at 10:41
  • @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). – 6502 Apr 06 '14 at 11:46
3

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;
}
sehe
  • 374,641
  • 47
  • 450
  • 633
  • +1 This is how I would do it in C++. Note: `using namespace std::chrono;` seems very worth it here. – smoothware Apr 05 '14 at 10:41
  • Changed the sample to actually print milliseconds since the epoch – sehe Apr 05 '14 at 10:52
  • Although [even MSVC, the compiler-that-always-lags-behing, had `` in VS2012](http://msdn.microsoft.com/en-us/library/hh567368(v=vs.110).aspx) – sehe Apr 05 '14 at 10:59
  • @LaszloPapp *This will not work without C++11 support* -- there no point to consider outdated standards, unless you're dealing with legacy code. – Walter Apr 05 '14 at 11:56
  • @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. – László Papp Apr 05 '14 at 11:57
  • 2
    Okay, time to calm down people. It's all useful information. No need to imply incompetence left or right – sehe Apr 05 '14 at 12:01
  • @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)... – Matthieu M. Apr 05 '14 at 14:00
  • @MatthieuM.: yes, I agree. – László Papp Apr 05 '14 at 15:39
  • 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); – Tad Sep 29 '14 at 14:43
  • @Tad your compiler might not be to update/have full support for std::chrono (yet): http://en.cppreference.com/w/cpp/chrono/high_resolution_clock. Depending on the resolution you desire, `system_clock` could be fine for your purposes – sehe Sep 29 '14 at 14:44
  • 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 – Tad Sep 29 '14 at 14:54
  • @Tad look at the question for context. Heck, here's a link for you http://docs.oracle.com/javase/7/docs/api/java/lang/System.html#currentTimeMillis() – sehe Sep 29 '14 at 14:56
1

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";
}
László Papp
  • 51,870
  • 39
  • 111
  • 135
  • What do seconds sinds the epoch have to do with things? – sehe Apr 05 '14 at 10:45
  • @sehe: read about System.currentTimeMillis in the original question, please. This may be [a good starting point](http://stackoverflow.com/questions/17271039/does-system-currenttimemillis-return-utc-time). – László Papp Apr 05 '14 at 10:46