CppCon 2016: Howard Hinnant “A <chrono> Tutorial"

Sdílet
Vložit
  • čas přidán 3. 10. 2016
  • CppCon.org
    -
    Presentation Slides, PDFs, Source Code and other presenter materials are available at: github.com/cppcon/cppcon2016
    -
    This talk starts very simple: with seconds. It explains exactly what a second is: what is under the hood, what it can do, and most importantly how and why it fails at compile time. Failing at compile time, instead of at run time is the critical design philosophy that runs through chrono.
    Slowly the talk adds complexity: other units of time duration, custom time durations, conversions among durations, points in time, etc. With each addition, another layer of the chrono onion is peeled away.
    By the end of the talk, you will understand both the importance of the simplicity of chrono, and the power available to you if you avoid the typical mistakes beginners make when first learning chrono. Even experts will find at least one undiscovered gem in this talk to take back to their own code. And you may see ways to transfer some of the chrono design principles into your own designs.
    People who attend this talk will be especially well prepared for my later talk about time zones.
    This talk is a prequel to my CppCon 2015 talk: • CppCon 2015: Howard Hi...
    -
    Howard Hinnant
    Senior Software Engineer, Ripple
    Lead author of several C++11 features including: move semantics, unique_ptr and . | | Lead author on three open source projects: A std::lib implementation: libcxx.llvm.org | An Itanium ABI implementation: libcxxabi.llvm.org | A date/time/timezone library: github.com/HowardHinnant/date
    -
    Videos Filmed & Edited by Bash Films: www.BashFilms.com
    *-----*
    Register Now For CppCon 2022: cppcon.org/registration/
    *-----*

Komentáře • 100

  • @ultradude5410
    @ultradude5410 Před 2 lety +27

    I had no idea was so straightforward!
    Every time I had seen it used, I got spooked by the long type names, but now I see it’s not that complicated to use!
    And an absolutely fantastic talk as well! It certainly is a great skill to make this much information easily digestible.

    • @CppCon
      @CppCon  Před 2 lety +5

      Very pleased that the tutorial was so helpful.

    • @Asdayasman
      @Asdayasman Před rokem +5

      "Every time I had seen it used, I got spooked by the long type names"
      That's the entirety of C++ for me lmao. "Oh, you missed a `const` while daring to use a STL type? Have three kilobytes of error messages with completely indecipherable types". Thanks GCC.

  • @origamibulldoser1618
    @origamibulldoser1618 Před 7 lety +93

    I used to think was okay. It's clearly better than okay.

    • @llothar68
      @llothar68 Před 7 lety +8

      Took me a while to got used to it, but i'm now a believer.

    • @origamibulldoser1618
      @origamibulldoser1618 Před 7 lety +23

      I guess in C++, one has to have experts explain to you why something is good or bad, because it's not immediately apparent to the average programmer. Take that for what you will.
      The talk is excellent though. Baby steps and very clearly explained.

    • @Merthalophor
      @Merthalophor Před 5 lety +11

      This appears to be a reoccurring pattern in C++ when introduced to new features & libs. First reaction is often "why would I?", but it usually ends with "how would I without?".

    • @user-rm2gh2gc5f
      @user-rm2gh2gc5f Před 2 lety

      @Mel FloranceAt first glance, yea))

    • @mattthorn4789
      @mattthorn4789 Před 2 lety

      I feel like they've forgot what "KISS" means. It's getting better by c++20, but still 😕

  • @christianczechP
    @christianczechP Před 7 lety +47

    chrono is one of the best things in recent C++ progress, which makes life much easier.

  • @EH-ty7ko
    @EH-ty7ko Před 4 lety +15

    Hands down, the best library tutorial I have ever seen.

  • @EgD996
    @EgD996 Před 7 lety +44

    Before watching this video, I never thought chrono is so good. It would be great if there is similar implementation for Si Units.

    • @VivekYadav-ds8oz
      @VivekYadav-ds8oz Před 5 lety +9

      I'm pretty sure people would've done that by now, it'd be so cool to just do something like
      Weights::Kilogram potatoes(5kg);
      if (potatoes >= 800g)
      {
      cout

    • @drslothy
      @drslothy Před 3 lety +3

      @@VivekYadav-ds8oz There's a few libraries for that... I've tried this one and I have mixed feelings but overall it was pretty good. github.com/nholthaus/units

  • @umnofff
    @umnofff Před 7 lety +15

    Never thought talking about seconds could be so interesting

  • @howardhinnant
    @howardhinnant Před 5 lety +30

    The epoch change for system_clock announced by VS in the Q&A period of this talk has been rescinded. And the C++20 draft spec now specifies the system_clock epoch to existing practice (1970) so that it can be officially depended upon.

    • @williamchamberlain2263
      @williamchamberlain2263 Před 5 lety +2

      Thanks for the update! Good that UTC really is U.

    • @mytech6779
      @mytech6779 Před 2 lety

      @@williamchamberlain2263 Well its really an Earth solar time coordinate. I would prefer they use international atomic time for system clocks and convert to UTC in software.It would allow better handling of leap seconds.Though I forget how it would effect handling of GPSS time but that is a pretty narrow case.

    • @mytech6779
      @mytech6779 Před 2 lety +1

      Good I am so tired of MS breaking standards to purposefully harm portability and increase vendor lock in. I know average home users fall for such shenanigans but I'm glad they get push back from the other end.

    • @ChristianBrugger
      @ChristianBrugger Před 8 měsíci

      UTC, atomic, and GPS clocks where added in C++20.

  • @ProfessorWaltherKotz
    @ProfessorWaltherKotz Před 2 lety +2

    This might the best presentation title I've ever seen. "It's about time" is just awesome!

  • @honey_sukesan
    @honey_sukesan Před 3 měsíci

    A really comprehensive "chrono" tutorial. Going to bookmark this.

  • @gavinchou3022
    @gavinchou3022 Před 5 lety +4

    Nice talk.
    It is mainly 2 parts:
    part 1, duraton:
    what duration is and what is under the hood and how much it cost (the same as builtin types)
    how to convert (self-defined) durations and how it works
    warn us when to use duration_cast, accuracy loss
    part 2, time point and clock:
    what is time point and time point operations, relation with duration -- tp +- tp => duration, tp +- duration => tp
    what is clocks, how they generate time point.

  • @emm1257
    @emm1257 Před 4 lety +4

    This talk was simply remarkable! Thank you Howard for this amazing presentation!

  • @cyrilemeka6987
    @cyrilemeka6987 Před 9 měsíci

    The explanation of the chrono library is so beautifully done, Thanks❤

  • @justinkovacich4037
    @justinkovacich4037 Před 4 lety +1

    This was very helpful, I was struggling through error codes and stack overflow answers that didn’t quite line up to what I needed. Thanks!!

  • @estebanduran6908
    @estebanduran6908 Před 5 lety +1

    This is such an amazing library.

  • @RupeshBhurke
    @RupeshBhurke Před 4 lety

    Wow! It's really a great header and even better video about it. Kudos!!

  • @wesofx8148
    @wesofx8148 Před 7 lety +15

    I am going to look for all sorts of excuses to use chrono now. This is great! :)

    • @howardhinnant
      @howardhinnant Před 7 lety +3

      Here is a way to make chrono so much more useful by extending it to calendars and timezones: github.com/HowardHinnant/date

    • @wesofx8148
      @wesofx8148 Před 7 lety +2

      Thanks, but I rarely have a need for those things. I did however use std::chrono and std::thread to quickly make a real-time terminal game prototype. I absolutely love having the ability to typedef custom durations :)

  • @snandi1603
    @snandi1603 Před rokem +1

    The best tutorial on Chrono.

  • @georgiossamaras5063
    @georgiossamaras5063 Před 7 lety +1

    Helpful tips in that video!

  • @knedlsepp
    @knedlsepp Před 7 lety +6

    I can only imagine how great a Boost.Units replacement based on the chrono library would be. But for now I'm looking forward to getting a standard datetime library.

  • @milespiano
    @milespiano Před 2 lety +1

    I LITERALLY just saw this dude on stack overflow.

    • @howardhinnant
      @howardhinnant Před 2 lety

      Did I forget to turn off my webcam again?! ;-)

  • @Tony-tu8uz
    @Tony-tu8uz Před rokem

    very good speech, thank you

  • @yrds96
    @yrds96 Před 2 měsíci

    I'm so used with convoluted date's library from other languages(I'm speaking of you javascript) . That I struggled with std::chrono trying to make things complicated without any reason. It's pretty straight forward, duration_cast it's being handy in my implementations.

  • @pruibiebehastoet1914
    @pruibiebehastoet1914 Před 3 lety +2

    A well-designed library.. Thank you

  • @jamesflames6987
    @jamesflames6987 Před 3 lety +2

    std::vector does value initialise it's elements. Therefore it will zero a vector of std::chrono::second or anything else by default.

  • @yuehuajia3176
    @yuehuajia3176 Před 6 lety

    excellent!

  • @timothyjohns8775
    @timothyjohns8775 Před 6 lety +2

    At 20:42, Howard says that cout is legacy code. Does anyone know what he means by this? A quick google search didn't help

    • @howardhinnant
      @howardhinnant Před 6 lety +12

      I might be able to help with that. ;-) I meant that `cout` is legacy with respect to ``. `` was introduced in C++11, whereas `cout` was introduced in C++98. So naturally `cout` knows nothing about ``. I hope to teach `` about `cout` in a future standard (wg21.link/p0355), and you can experiment with that today if you like (github.com/HowardHinnant/date).
      But my main point in the presentation is that you can interface with any legacy code (code which was not designed to work with ``) by using the `.count()` member function of durations.

    • @timothyjohns8775
      @timothyjohns8775 Před 6 lety

      Ah, thanks for clearing that up! And thanks for the great talk. I was able to clean up the timing code in my personal project quite a bit after learning how is really supposed to be used.

  • @adrianf.5847
    @adrianf.5847 Před rokem

    At about 50:30, wouldn't it induce an error if we chose a duration type where the Fraction is too large, like hours, because the implicit conversion might be lossy, depending on the implementation of C++?

    • @howardhinnant
      @howardhinnant Před rokem +1

      If the conversion is to a duration with a floating-point rep, then you can get round-off (precision) error, but not truncation error. For example if you are converting integral nanoseconds to floating-point hours, it will first convert the nanoseconds to the floating point type, then divide by 3.6e+12. If you are converting to integral hours, there will be a truncation error (as integer division does), and you will have to use duration_cast to explicitly ask for such a truncating conversion. I.e. it will take the number of nanoseconds and divide by 3'600'000'000'000.

  • @aniketbisht2823
    @aniketbisht2823 Před 19 dny

    C++ libraries like std::chrono are the biggest argument for C++ against C. All the safeties and convenience without any loss of performance.

  • @wpavada3247
    @wpavada3247 Před 7 lety +4

    One thing i would want is that the output stream operator

    • @howardhinnant
      @howardhinnant Před 7 lety +8

      You may find this free, open-source, header-only library useful: howardhinnant.github.io/date/chrono_io.html

    • @ccreutzig
      @ccreutzig Před 6 lety +1

      Localized, of course ...

  • @adrianf.5847
    @adrianf.5847 Před 11 měsíci

    Interestingly, time points and durations are analogous to affine space and vector space.

  • @imlovinit1232
    @imlovinit1232 Před 11 měsíci

    Is there a way to use chrono and exceed the defacto ranges? Ie, can you easily, neatly, and portably create a Nanoseconds duration capable of >146 /292 years

    • @howardhinnant
      @howardhinnant Před 7 měsíci +1

      Yes. Use a bigger number. For example if your platform has a 128 bit integer, you can use that as the rep in duration. Otherwise you can get a 128 bit emulator from libraries such as boost::multiprecision and use that in duration.

  • @srenh-p3798
    @srenh-p3798 Před rokem

    When he says "in all 3 implementations they are the same thing" at 48:25 . Is the 3 refering to gcc, clang and msvc and their implementaitons of c++ std lib?

  • @JohnNelsonNumber2
    @JohnNelsonNumber2 Před rokem

    So if I'm stuck in a C++2011/2014 project, how can I use chrono to calculate the number of months between two time points? It's not as simple as using 30 days or 2628000 seconds because it really depends on WHICH months are between the two time points.

    • @howardhinnant
      @howardhinnant Před rokem

      You can use my date.h library to calculate the number of months between two time points in C++11/14/17.

  • @syntaxed2
    @syntaxed2 Před 3 lety +1

    I would love to see a codebase that overflows chrono::seconds::max() :D

  • @cariyaputta
    @cariyaputta Před 7 lety +1

    I wanna ask an error I getting while using
    chrono::high_resolution_clock::time_point tstart, tstop;
    then
    tstart = chrono::high_resolution_clock::now;
    but I get the error "No operator "=" matches these operands. Operand types are std::chrono::steady_clock::time_point = std::chrono::steady_clock::time_point ()"

    • @cariyaputta
      @cariyaputta Před 7 lety +2

      Yeah it's a stupid typo, I cry every time I see this thread.

    • @howardhinnant
      @howardhinnant Před 7 lety +5

      Anyone who tells you they've never made a stupid coding error is either a liar or has never coded. :-)

  • @rohitrajora7202
    @rohitrajora7202 Před rokem

    Is platform independent????
    Want it to work on both windows and linux systems.

    • @howardhinnant
      @howardhinnant Před rokem

      is portable C++. It works on Windows, Linux, macOS, Android, iOS ... anywhere you can target with a modern C++ compiler.

  • @ThatMontmorency
    @ThatMontmorency Před rokem

    Um... What? What does he mean when he talks about a million-element vector not initializing every element to zero? Any standard container _will_ forcefully value-initialize all of its elements, regardless of whether you asked it to or not. And for an object with a defaulted default constructor, it will involve zeroing everything out (at least). You can't prevent it unless you customize the allocator.

  • @victoreijkhout6146
    @victoreijkhout6146 Před 2 lety +1

    Nice talk. Starts with a lie in the first minute: the "using namespace std::chrono" is not just saving space: the notation "5s" does not work without it.

  • @ptaszor9779
    @ptaszor9779 Před 3 lety

    33:29 isn't it "Instantiation" and not "specialization"?

  • @HimanshuRastogizzz
    @HimanshuRastogizzz Před 3 lety

    Before watching this talk I use lots of .count(), duration_cast and high_resolution_clock

  • @brotherhood_of_harkonnen

    How can i assign a custom year, month and day to a time_point?

    • @howardhinnant
      @howardhinnant Před 2 lety

      You can convert a year, month and day to a system_clock-based time_point with: sys_days tp = year{y}/m/d; where y, m and d have type int. sys_days is a type alias for time_point, aka a time_point with days precision based on system_clock. If you want a finer precision time_point, say system_clock::time_point you can: system_clock::time_point tp = sys_days{year{y}/m/d}; tp will point to 00:00:00.000000 UTC of that year, month and day.

  • @VivekYadav-ds8oz
    @VivekYadav-ds8oz Před 5 lety +1

    Just one question though, why is it named "chrono" ?

    • @eragg0
      @eragg0 Před 5 lety +3

      Chrono is abbreviation of word chronology. For further reading en.wikipedia.org/wiki/Chronology

    • @Aither-
      @Aither- Před 3 lety

      @@pepxycolla Not sure Chronos was a titan. You may confuse him with the titan Cronos, father of Zeus.

    • @tissuepaper9962
      @tissuepaper9962 Před 2 lety

      @@Aither- Chronos and Cronus were one and the same for like 2000 years.

  • @ocrap7
    @ocrap7 Před 2 lety

    Why does he say cout is legacy code? Is there an alternative?

    • @howardhinnant
      @howardhinnant Před 2 lety +1

      Because in C++11/14/17 cout and the rest of the streaming library is unaware of chrono. I.e. one can't stream out chrono types/values. This changes in C++20. Now you can stream out chrono types. This eliminates a large part of the need for the duration member function .count().

  • @jamesflames6987
    @jamesflames6987 Před 3 lety +2

    allows to to replace confusing code like:
    time(nullptr)
    with:
    std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()).count()

    • @kebien6020
      @kebien6020 Před 3 lety +1

      I mean, if you really need to get the time since unix epoch in seconds that often you can just create a function for that.
      auto now() -> std::chrono::seconds
      {
      using namespace std::chrono;
      // Purposely not calling .count to avoid defeating the purpose of the library
      return duration_cast(system_clock::now().time_since_epoch());
      }
      Btw, time(nullptr) *is* confusing. You have to lookup documentation in order to know the units it returns. Also, I can't find the rationale behind the function having an out param and also a return value for the same thing.

    • @Unit_00
      @Unit_00 Před 2 lety +1

      @@kebien6020 the worst documentation I've ever seen in terms of clarity is chrono's

    • @Unit_00
      @Unit_00 Před 2 lety +2

      @@kebien6020 "You have to lookup documentation in order to know..." Yeah, you have to lookup the documentation in order to know how it works. You are pretending like you don't have to do this with chrono as well (or with literally every single concept in programming).

  • @victoreijkhout6146
    @victoreijkhout6146 Před 2 lety

    Unnumbered (why?) slide at 3:30: he claims that seconds has an int internally. Did this change to float? I can "3.14 * 3s" and the "count" is 9.42. See also 12:20.
    EDIT. Ah. He later admits that that was a white lie.
    After 50:00 there is a lot of missing information. (I hate how he keeps saying "just" and "simply". Unless I exactly type his examples I get a ton of compilation errors. It is not at all clear how this stuff generalizes.) Why can you "nanoseconds{t1-t0}" but you have to cast to get anything else?

    • @howardhinnant
      @howardhinnant Před 2 lety

      > Why can you "nanoseconds{t1-t0}" but you have to cast to get anything else?
      Because t1 and t0 have nanosecond precision. "Everything else" is coarser than nanoseconds, assuming integral representation. So the library doesn't let you loose information implicitly. To truncate nanoseconds to microseconds (for example), you have to use a named conversion since it is a lossy conversion. Going the other way, microseconds to nanoseconds, is loss-less. And that can happen implicitly.

  • @nd685
    @nd685 Před 3 lety

    Time a function 49:56

  • @anfield6321
    @anfield6321 Před 7 lety +7

    Can a man be this clear?

  • @wesofx8148
    @wesofx8148 Před 7 lety +1

    I laughed when he introduced duration_cast. That's so standard library!

  • @Nandarion
    @Nandarion Před 5 lety +2

    one problem is that is too difficult to convert durations from string or to string.

    • @howardhinnant
      @howardhinnant Před 5 lety +3

      We hope to address that in C++20. Not only with durations, but time_points as well.

  • @adrianf.5847
    @adrianf.5847 Před 11 měsíci

    I was just rewatching this, and voilà:
    #include
    #include
    using namespace std::literals::chrono_literals;
    void f(std::chrono::milliseconds d) {
    std::cout

  • @weekendwarrior3420
    @weekendwarrior3420 Před rokem

    duration_cast should have been called "truncation_cast".

    • @howardhinnant
      @howardhinnant Před rokem

      Agreed. Or maybe just truncate. Ditto for time_point_cast. You live and learn.

  • @adrianf.5847
    @adrianf.5847 Před rokem

    "If you overflow, you've got issues." I suppose that astrophysicists may be mildly offended by this line...

    • @howardhinnant
      @howardhinnant Před rokem

      One can increase range either by reducing precision (e.g. use microseconds instead of nanoseconds), or by changing the rep to something with a larger range (e.g. __int128). I'm sure astrophysicists are familiar with such tradeoffs in their computations, with or without chrono.

    • @adrianf.5847
      @adrianf.5847 Před rokem

      @@howardhinnant OK, let's just say that an astrophysicist might use an implementation where the range of SECONDS is as large as desired, or else use a custom duration...

    • @adrianf.5847
      @adrianf.5847 Před rokem

      @@howardhinnant Just to clarify matters, I don't at all dislike chrono, to the contrary, I think it's fine work.

  • @adityaranigaon
    @adityaranigaon Před 2 lety

    Chrono made game programming simpler

  • @mattthorn4789
    @mattthorn4789 Před 2 lety

    I really like the path that STL takes since c++11, but this chrono thing is the single (and maybe only) part of it, that is straight up sh*t

  • @KansasFashion
    @KansasFashion Před 6 lety +1

    Too long

  • @georgecarlin2656
    @georgecarlin2656 Před 6 lety +4

    Over engineered and the problem overrated. Never had an issue dealing with time in other toolkits like Qt.