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

Поділитися
Вставка
  • Опубліковано 17 жов 2024

КОМЕНТАРІ • 98

  • @origamibulldoser1618
    @origamibulldoser1618 8 років тому +95

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

    • @llothar68
      @llothar68 8 років тому +8

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

    • @origamibulldoser1618
      @origamibulldoser1618 8 років тому +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 6 років тому +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?".

    • @valerii-barabanov-vvb
      @valerii-barabanov-vvb 3 роки тому

      @Mel FloranceAt first glance, yea))

    • @mattthorn4789
      @mattthorn4789 2 роки тому

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

  • @ultradude5410
    @ultradude5410 2 роки тому +28

    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  2 роки тому +4

      Very pleased that the tutorial was so helpful.

    • @Asdayasman
      @Asdayasman Рік тому +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.

  • @christianczechP
    @christianczechP 8 років тому +48

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

  • @EH-ty7ko
    @EH-ty7ko 4 роки тому +15

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

  • @EgD996
    @EgD996 7 років тому +45

    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 5 років тому +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 4 роки тому +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 8 років тому +15

    Never thought talking about seconds could be so interesting

  • @ProfessorWaltherKotz
    @ProfessorWaltherKotz 2 роки тому +2

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

  • @gavinchou3022
    @gavinchou3022 5 років тому +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.

  • @howardhinnant
    @howardhinnant 6 років тому +31

    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 5 років тому +2

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

    • @mytech6779
      @mytech6779 2 роки тому

      @@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 2 роки тому +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 Рік тому

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

  • @emm1257
    @emm1257 4 роки тому +4

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

  • @snandi1603
    @snandi1603 2 роки тому +1

    The best tutorial on Chrono.

  • @cyrilemeka6987
    @cyrilemeka6987 Рік тому

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

  • @honey_sukesan
    @honey_sukesan 8 місяців тому

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

  • @wesofx8148
    @wesofx8148 7 років тому +15

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

    • @howardhinnant
      @howardhinnant 7 років тому +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 7 років тому +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 :)

  • @justinkovacich4037
    @justinkovacich4037 5 років тому +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!!

  • @knedlsepp
    @knedlsepp 8 років тому +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.

  • @adrianf.5847
    @adrianf.5847 Рік тому +1

    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 Рік тому +2

      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.

  • @timothyjohns8775
    @timothyjohns8775 6 років тому +3

    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 6 років тому +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 6 років тому

      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.

  • @JohnNelsonNumber2
    @JohnNelsonNumber2 Рік тому

    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 Рік тому

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

  • @milespiano
    @milespiano 2 роки тому +1

    I LITERALLY just saw this dude on stack overflow.

    • @howardhinnant
      @howardhinnant 2 роки тому

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

  • @cariyaputta
    @cariyaputta 7 років тому +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 7 років тому +2

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

    • @howardhinnant
      @howardhinnant 7 років тому +5

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

  • @imlovinit1232
    @imlovinit1232 Рік тому

    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 Рік тому +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.

  • @rohitrajora7202
    @rohitrajora7202 Рік тому

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

    • @howardhinnant
      @howardhinnant Рік тому

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

  • @estebanduran6908
    @estebanduran6908 5 років тому +1

    This is such an amazing library.

  • @jamesflames6987
    @jamesflames6987 4 роки тому +2

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

  • @srenh-p3798
    @srenh-p3798 Рік тому

    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?

  • @ThatMontmorency
    @ThatMontmorency Рік тому

    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 2 роки тому +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.

  • @RupeshBhurke
    @RupeshBhurke 4 роки тому

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

  • @yrds96
    @yrds96 7 місяців тому

    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.

  • @brotherhood_of_harkonnen
    @brotherhood_of_harkonnen 2 роки тому

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

    • @howardhinnant
      @howardhinnant 2 роки тому

      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.

  • @wpavada3247
    @wpavada3247 7 років тому +4

    One thing i would want is that the output stream operator

    • @howardhinnant
      @howardhinnant 7 років тому +8

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

    • @ccreutzig
      @ccreutzig 7 років тому +1

      Localized, of course ...

  • @pruibiebehastoet1914
    @pruibiebehastoet1914 3 роки тому +2

    A well-designed library.. Thank you

  • @adrianf.5847
    @adrianf.5847 Рік тому

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

  • @aniketbisht2823
    @aniketbisht2823 5 місяців тому

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

  • @VivekYadav-ds8oz
    @VivekYadav-ds8oz 5 років тому +1

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

    • @eragg0
      @eragg0 5 років тому +3

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

    • @Aither-
      @Aither- 3 роки тому

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

    • @tissuepaper9962
      @tissuepaper9962 2 роки тому

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

  • @Tony-tu8uz
    @Tony-tu8uz Рік тому

    very good speech, thank you

  • @syntaxed2
    @syntaxed2 4 роки тому +1

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

  • @victoreijkhout6146
    @victoreijkhout6146 2 роки тому

    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 2 роки тому

      > 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.

  • @ocrap7
    @ocrap7 3 роки тому

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

    • @howardhinnant
      @howardhinnant 3 роки тому +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().

  • @HimanshuRastogizzz
    @HimanshuRastogizzz 3 роки тому

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

  • @ptaszor9779
    @ptaszor9779 3 роки тому

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

  • @Nandarion
    @Nandarion 5 років тому +2

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

    • @howardhinnant
      @howardhinnant 5 років тому +3

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

  • @georgiossamaras5063
    @georgiossamaras5063 7 років тому +1

    Helpful tips in that video!

  • @anfield6321
    @anfield6321 7 років тому +7

    Can a man be this clear?

  • @wesofx8148
    @wesofx8148 7 років тому +1

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

  • @jamesflames6987
    @jamesflames6987 4 роки тому +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 4 роки тому +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 2 роки тому

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

    • @Unit_00
      @Unit_00 2 роки тому +1

      @@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).

  • @yuehuajia3176
    @yuehuajia3176 6 років тому

    excellent!

  • @nd685
    @nd685 4 роки тому

    Time a function 49:56

  • @adrianf.5847
    @adrianf.5847 Рік тому

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

  • @adrianf.5847
    @adrianf.5847 Рік тому

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

    • @howardhinnant
      @howardhinnant Рік тому

      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 Рік тому

      @@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 Рік тому

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

  • @adityaranigaon
    @adityaranigaon 3 роки тому

    Chrono made game programming simpler

  • @mattthorn4789
    @mattthorn4789 2 роки тому

    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 6 років тому +1

    Too long

  • @georgecarlin2656
    @georgecarlin2656 6 років тому +4

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