50

I have a stupid problem. I try to switch to the c++11 headers and one of those is chrono. But my problem is that I cant cout the result of time operations. For example:

auto t=std::chrono::high_resolution_clock::now();
cout<<t.time_since_epoch();

gives:

initializing argument 1 of ‘std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char, _Traits = std::char_traits<char>, _Tp = std::chrono::duration<long int, std::ratio<1l, 1000000l> >]’ ... /usr/include/c++/4.6/ostream

cout<<(uint64_t)t.time_since_epoch();

gives invalid cast

R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277

4 Answers4

52

As others have noted, you can call the count() member function to get the internal count.

I wanted to add that I am attempting to add a new header: <chrono_io> to this library. It is documented here. The main advantage of <chrono_io> over just using count() is that the compile-time units are printed out for you. This information is of course obtainable manually, but it is much easier to just have the library to it for you.

For me, your example:

#include <iostream>
#include <chrono_io>

int main()
{
    auto t = std::chrono::high_resolution_clock::now();
    std::cout << t.time_since_epoch() << '\n';
}

Outputs:

147901305796958 nanoseconds

The source code to do this is open source and available at the link above. It consists of two headers: <ratio_io> and <chrono_io>, and 1 source: chrono_io.cpp.

This code should be considered experimental. It is not standard, and almost certainly will not be standardized as is. Indeed preliminary comments from the LWG indicate that they would prefer the default output to be what this software calls the "short form". This alternative output can be obtained with:

std::cout << std::chrono::duration_fmt(std::chrono::symbol)
          << t.time_since_epoch() << '\n';

And outputs:

147901305796958 ns

Update

It only took a decade, but C++20 now does:

#include <chrono>
#include <iostream>

int main()
{
    auto t = std::chrono::high_resolution_clock::now();
    std::cout << t.time_since_epoch() << '\n';
}

Output:

147901305796958ns
Howard Hinnant
  • 206,506
  • 52
  • 449
  • 577
  • By my original question you can guess I'm not a cpp guru :), but why dont you overload time_since_epoch t.time_since_epoch(chrono::print_unit) – NoSenseEtAl Oct 25 '11 at 15:32
  • 1
    At this time there are very few chrono gurus on the planet. So you're doing just fine! :-) I may have misunderstood your suggestion, but it sounds like you're suggesting to set the units of the duration, at least for printing purposes. This could cause inexact conversions to silently take place, which the design of chrono has gone to trouble to prevent. I.e. different duration types will only implicitly convert if there is no truncation error made by the conversion. – Howard Hinnant Oct 25 '11 at 17:01
  • what i meant is that you overload the function and that overload returns std::string that has unit sticked to the end. or you could have different params instead of print_unit have print_nano, print_milli or even print_unit... or just to update (w)cout to take (with << ) print_units...(aka std::cout< – NoSenseEtAl Oct 25 '11 at 17:58
  • 1
    I see, thanks. It is traditional in C++ to print things by using `stream << object` (it was the first thing you tried). And it is best to stick with tradition so as not to confuse everyone (unless you want to design an entirely new I/O system). As for the separate header, I could be talked into putting all of the functionality into ``. It's an engineering tradeoff (increases compile time for many use cases). Standardization schedule: We're very lucky to see `` in C++11. We very nearly got C12's `xtime` instead. Standardization is much harder than design. – Howard Hinnant Oct 25 '11 at 18:15
  • I understand, sometimes(like when I read that original STL had to be reduced for political reasons , or when I see members of the committee giving lectures at Google and saying you know could you spare a couple hundred k USD ) I wish that Bjorne stops with ISO stuff and do stuff with help of MS, Google,... :) – NoSenseEtAl Oct 26 '11 at 11:45
  • any news regarding this header? I did not hear anything about this recently regarding standardization – NoSenseEtAl Jul 30 '14 at 12:10
  • Sorry, no news. If enough people make enough noise about any given issue, it happens, else it doesn't. – Howard Hinnant Jul 30 '14 at 14:20
  • 1
    Sad to see that searching for proposals about chrono i/o don't bring up anything except for this answer. We could really need such an addition. Also fixed your links since they were broken. – AliciaBytes Dec 08 '14 at 19:56
  • Draft proposal including formatting for durations: https://howardhinnant.github.io/date/d0355r1.html – Howard Hinnant Oct 10 '16 at 20:59
  • 1
    Printing durations is now in the C++20 draft spec, though the exact syntax and format differ from those shown in my answer. – Howard Hinnant Nov 14 '18 at 21:22
  • Howard thank you for updating the answer with C++20 version! If it does not work for somebody: AFAIK only MSVC implements it at the moment. – NoSenseEtAl Sep 25 '21 at 11:11
25

A quick google search found this page: http://en.cppreference.com/w/cpp/chrono/duration, where you can find an example of printing a duration.

Edit: it got moved to http://en.cppreference.com/w/cpp/chrono/duration/duration_cast

CC.
  • 2,839
  • 2
  • 19
  • 13
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 1
    i use that wiki, but I never found it... tnx – NoSenseEtAl Oct 25 '11 at 12:34
  • 2
    Beware that the linked example is different in that it prints the difference between two invocations of now(), and does not involve time_since_epoch(). In fact, time_since_epoch leaves the actual epoch unspecified (it depends on the clock from which you got the time_point). So, in short, the duration you're printing is kind of meaningless without accompanied information about the epoch. – Johan Boulé Apr 05 '15 at 19:12
13

If you want timing in resolution of milliseconds this is how you can do it:

auto t1 = std::chrono::high_resolution_clock::now();
//process to be timed
auto t2 = std::chrono::high_resolution_clock::now();
std::cout << "process took: "
    << std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1).count()
    << " milliseconds\n";

Don't forget to add among the included headers:

#include <chrono> //timing
Yonatan Simson
  • 2,395
  • 1
  • 24
  • 35
7

Not sure what you expect from this cast, maybe you wanted t.time_since_epoch().count()?

Michael Krelin - hacker
  • 138,757
  • 24
  • 193
  • 173