Timing in C++

แชร์
ฝัง
  • เผยแพร่เมื่อ 27 มิ.ย. 2024
  • Patreon ► / thecherno
    Instagram ► / thecherno
    Twitter ► / thecherno
    Discord ► thecherno.com/discord
    chrono reference ► en.cppreference.com/w/cpp/chrono
    Series Playlist ► thecherno.com/cpp
    Thank you to the following Patreon supporters:
    - Dominic Pace
    - Kevin Gregory Agwaze
    - Sébastien Bervoets
    - Tobias Humig
    - Peter Siegmund
    - Kerem Demirer
    Gear I use:
    -----------------
    BEST laptop for programming! ► geni.us/pakTES
    My FAVOURITE keyboard for programming! ► geni.us/zNhB
    FAVOURITE monitors for programming! ► geni.us/Ig6KBq
    MAIN Camera ► geni.us/t6xyDRO
    MAIN Lens ► geni.us/xGoDWT
    Second Camera ► geni.us/CYUQ
    Microphone ► geni.us/wqO6g7K

ความคิดเห็น • 199

  • @lewisb8634
    @lewisb8634 6 ปีที่แล้ว +228

    When you started this series, I thought I had a decent enough understanding of C++ and didn't need to watch it. I've watched episodes anyway because I enjoy your video style. I've learned so much though. For example, I knew about std::chrono and some profiling methods before watching this, but I've never seen anyone wrap it so nicely in an object and use scope lifetime to their advantage so well. I'll make use of this from now on. So many great tricks to pick up from watching this series! Thanks as always Cherno! :)

    • @danielesquivel3155
      @danielesquivel3155 3 ปีที่แล้ว +1

      yes

    • @ryansullivan3085
      @ryansullivan3085 3 ปีที่แล้ว +1

      same

    • @ataladin87
      @ataladin87 2 ปีที่แล้ว +1

      Wow, You just sounded arrogant, then Cherno Kicked you into Humbleness.

    • @joedartonthefenderbass
      @joedartonthefenderbass 10 หลายเดือนก่อน +8

      @@ataladin87 no they didn't? they just thought they had a good understanding of c++

  • @scrub6515
    @scrub6515 6 ปีที่แล้ว +48

    This series is so fucking good, keep it up

  • @fishyperil2153
    @fishyperil2153 6 ปีที่แล้ว +90

    #include
    #include

  • @yee3135
    @yee3135 6 ปีที่แล้ว +39

    Another great video, love that you're using RAII, it's really the best c++ feature.
    btw you could have used "std::chrono::duration_cast" for your ms conversion ;)

  • @damianrivas
    @damianrivas 5 ปีที่แล้ว

    Beautiful presentation. Thank you!

  • @vertinog
    @vertinog 6 ปีที่แล้ว +74

    In cygwin I had to use
    _std::chrono::high_resolution_clock::time_point_
    instead of
    _std::chrono::time_point_
    Just an FYI for anyone getting the error that there is no _'operator='_ on std::chorno::time_point you may want to try the high resolution one.

    • @almawet
      @almawet 6 ปีที่แล้ว

      I had same error in qmake. Thanks!

    • @vijay6877
      @vijay6877 6 ปีที่แล้ว

      Justin Chrysler Thanks.

    • @vijay6877
      @vijay6877 6 ปีที่แล้ว

      Justin Chrysler I want to know the reason if you know please let me know.

    • @vertinog
      @vertinog 6 ปีที่แล้ว +1

      I would assume it has something to do with the way the chrono library is built for each compiler. Each compiler implements the standards differently.

    • @valizeth4073
      @valizeth4073 5 ปีที่แล้ว +3

      It's the same on the gcc compilers.

  • @sundarrajn1003
    @sundarrajn1003 6 ปีที่แล้ว

    excellent as always..hope u do more of these more often.

  • @andrewgundlach2905
    @andrewgundlach2905 6 ปีที่แล้ว +1

    Love your videos man.

  • @RandomGuyyy
    @RandomGuyyy 6 ปีที่แล้ว +5

    This series is really helpful and I'm looking forward to the game engine series.

  • @q_rsqrt5140
    @q_rsqrt5140 6 ปีที่แล้ว +136

    std::endl is slow beacuse for each line it must flush buffer

    • @MrCoder101
      @MrCoder101 5 ปีที่แล้ว +5

      On my pc, removing endl and adding
      actually made it slower... good 50% slower. weird

    • @them4309
      @them4309 5 ปีที่แล้ว +2

      @Peterolen It would be important if you are copying large data files or save files or outputting some kind of long report to be read later.

    • @sacripudding4586
      @sacripudding4586 3 ปีที่แล้ว

      @@them4309 Would have to be a pretty long report to be fair. I would imagine the report would be written to file though as logging it is probably super important.

    • @anthonysteinerv
      @anthonysteinerv 2 ปีที่แล้ว

      What does flushing the buffer means?

    • @lucasgroves137
      @lucasgroves137 ปีที่แล้ว +1

      @@anthonysteinerv He's from New Zealand... they call the toilet the buffer. 👍

  • @XolifreX
    @XolifreX 6 ปีที่แล้ว

    Awesome video! Never knew about this chrone library. Super handy! :)

  • @ZestyMuffins
    @ZestyMuffins 6 ปีที่แล้ว +6

    i'd like to hear you talk about callbacks in programming/c++. great videos

  • @alexanderrose1583
    @alexanderrose1583 3 ปีที่แล้ว

    The Timer structure is a brilliant idea. Thanks!

  • @hamidmajidi5103
    @hamidmajidi5103 5 ปีที่แล้ว

    So great tutorial... Good luck on your way!! Thank you.

  • @oamost
    @oamost 6 ปีที่แล้ว

    Good to see you back, Master!

  • @markwiedeman7271
    @markwiedeman7271 4 ปีที่แล้ว

    Thank you, this was a big help

  • @muhammadtaimourafzal5285
    @muhammadtaimourafzal5285 3 ปีที่แล้ว +6

    Cherno: You are using c++ because you obviously care about performance
    ME: Nah , i just want my code to run !!!

  • @EnrageGames
    @EnrageGames 6 ปีที่แล้ว +19

    Very interested to see your upcoming game engine series.

  • @DavePaddy
    @DavePaddy 3 ปีที่แล้ว

    This helped me a lot, thanks!

  • @ranasalman4972
    @ranasalman4972 5 หลายเดือนก่อน +1

    Great Video Bro 👍👍

  • @h.hristov
    @h.hristov 6 ปีที่แล้ว

    Thanks for this video

  • @ttoktassynov
    @ttoktassynov 4 ปีที่แล้ว

    Thank you !

  • @MCoreLabs
    @MCoreLabs 6 ปีที่แล้ว

    This video is right on time

  • @filipecoelho3383
    @filipecoelho3383 3 ปีที่แล้ว +1

    Muito obrigado!

  • @MrDevned
    @MrDevned ปีที่แล้ว

    The best c++ chanel good job

  • @mohannadrababah757
    @mohannadrababah757 ปีที่แล้ว

    I really liked this video .. like I pressed the Like button with full of thankfulness ... thank you so much for this explanation ❤❤

  • @Maniclout
    @Maniclout 2 ปีที่แล้ว

    Awesome video

  • @oblivionronin
    @oblivionronin 6 ปีที่แล้ว +1

    As always awesome video, love em all ! I know its some deep stuff but do you have any plans to cover the SIMD (single instruction multiple data ) instruction set ?
    Keep up the good work !

  • @JDStone20
    @JDStone20 6 ปีที่แล้ว

    Great series! Consice with still being in-depth, plus functional examples.

  • @MIXTraining
    @MIXTraining 6 ปีที่แล้ว

    This lessons are great thank you, when is the Game Engine series starting? cheers!!

  • @highbrAned
    @highbrAned 6 ปีที่แล้ว +70

    TheChronoProject
    =)

  • @______373
    @______373 2 ปีที่แล้ว

    me: *seeing a C++ tutorial*
    me: *opening this tutorial*
    cherno's video: *appears*
    me: *without watching to the end instantly clicking on cherno's video*

  • @The-Dev-Ninja
    @The-Dev-Ninja 3 ปีที่แล้ว

    Bravissimo

  • @BlackJar72
    @BlackJar72 6 ปีที่แล้ว

    This could be so useful to me, there are some functions I've been working on that really need profiling.

  • @DibsOnMe
    @DibsOnMe 4 ปีที่แล้ว +1

    This is genius.

  • @zulfiqarahmad9394
    @zulfiqarahmad9394 6 ปีที่แล้ว

    Hi love your videos. Please make video on different callback methods in c++

  • @gamingparlour1732
    @gamingparlour1732 4 ปีที่แล้ว +5

    those who are facing issue with chrono::time_point should use chrono::time_point....I think this issue is due to difference in OS( I have linux mint,cherno is using windows)..

    • @Jacklawi
      @Jacklawi 4 ปีที่แล้ว

      This solved it for me. But I was using Windows as well. I think it may be a different VC++ compiler than what Cherno was using.

    • @bluehornet6752
      @bluehornet6752 4 ปีที่แล้ว

      Or you can just change to use the high_resolution_clock as the templated type given to the time_point constructor. Then the now() methods are returning the same type as the time_point objects (start, end) are expecting. For added fun, I suppose you could also try to cast the steady_clock object to a high_resolution_clock object, and see if that works...lol. Talk about ugly syntax!

  • @sunday-thequant8477
    @sunday-thequant8477 3 ปีที่แล้ว

    Gold here!

  • @elsharaawy8662
    @elsharaawy8662 4 ปีที่แล้ว

    Hero

  • @taehongjeong3262
    @taehongjeong3262 6 ปีที่แล้ว +4

    3:23 literally, it is enlightening moment!

    • @eyesight2073
      @eyesight2073 3 ปีที่แล้ว

      😳 what just happened there?
      how 1s is a valid value?

  • @vhxhao
    @vhxhao 3 ปีที่แล้ว

    #include
    will do everything for you

  • @kledmainn4432
    @kledmainn4432 6 ปีที่แล้ว +6

    Can u make a video about the "constexpr" stuff?I can't understand how it actualy works .Ty

  • @Rakesh-yu1pb
    @Rakesh-yu1pb 4 ปีที่แล้ว

    what a cool ass guy

  • @neilbalch
    @neilbalch 6 ปีที่แล้ว

    Excellent video! Up until now, I thought you could only use std::this_thread::sleep_for once you were on a separate thread. I guess I never thought about how the main thread is a thread too.... :)

  • @adambruce4284
    @adambruce4284 6 ปีที่แล้ว +5

    @TheChernoProject Just curious how well informed you are with other languages? Obviously the way you think about programming allows you to pick up other ones really quickly but how proficient are you say with Python? Or Javascript? What other things have you learnt while being at EA not specifically related to c++?

  • @bloodwolf8307
    @bloodwolf8307 4 ปีที่แล้ว

    cool

  • @al7aro
    @al7aro 3 ปีที่แล้ว

    What's overhead? Thank you for this series btw

  • @tusharharsora7901
    @tusharharsora7901 6 ปีที่แล้ว +2

    The endl keyword is Slow Because Is Flushes all The Output Buffer And Then Put Newline

  • @TheHLotus
    @TheHLotus 6 ปีที่แล้ว +10

    How was GDC? anything interesting to share with us?

    • @BlackJar72
      @BlackJar72 6 ปีที่แล้ว +2

      Woah! Wish I was there -- procedural generation is, one of my favorite topics / things to experiment with.

  • @huyvole9724
    @huyvole9724 6 ปีที่แล้ว

    Hi Cherno ... I have idea, right click name project and click Properties -> Linker -> System -> SubSystem ... choose Console and OK then your console will stop without infinite loop, etc ...

  • @lKOKOLIZO
    @lKOKOLIZO 6 ปีที่แล้ว

    Hey Cherno did you talked before about how to handle exceptions??

  • @EndAtDay
    @EndAtDay 5 ปีที่แล้ว +6

    If you have the type error during struct, change std::chrono::time_point start, end; to std::chrono::time_point start, end;

    • @ianc8846
      @ianc8846 3 ปีที่แล้ว +1

      THANK YOU

  • @trinetram707
    @trinetram707 4 ปีที่แล้ว +1

    Great video but can you tell me what value does now() return

  • @Saturate0806
    @Saturate0806 6 ปีที่แล้ว +2

    Hey! Can you do an operator overloading video

  • @HarunAlHaschisch
    @HarunAlHaschisch 3 ปีที่แล้ว

    the difference in runtime for me was negligable / all over the place so I increased the for loop to 1000 and tried a few times and interestingly, in 64bit release mode I get around 720ms for the std::endl solution and 420ms for the
    solution. Intel i7 7700K btw.

  • @carloscao9193
    @carloscao9193 4 ปีที่แล้ว

    For Timer struct fail to compile:
    I changed 7th line, and it worked.
    std::chrono::time_point start, end;

  • @DanielsThierry
    @DanielsThierry 3 ปีที่แล้ว

    hey,
    Thanks for this insight. I enjoyed it.
    I was wondering if "time" could be slowed down or speed up.
    as example, day and night rhythm.
    but i want to slow time down from real time and speed it up to 200x real time.
    I want to incorporate this into a project.

    • @Unit_00
      @Unit_00 2 ปีที่แล้ว

      the timing libraries will only tell you about real life timing (how many seconds, miliseconds, etc). What you are describing has to do with how you implement your program loop.

  • @The2Coolest2
    @The2Coolest2 4 ปีที่แล้ว

    Hi. I have a timer for a speed calculation. I have the timing in seconds that I then convert to a double . I would like a milliseconds result. I feel just do *1000 is a bit sloppy. Any advice? Thanks.

    • @masheroz
      @masheroz 2 ปีที่แล้ว

      Unless there is a specific millisecond function, then that is the way to do it.

  • @23trekkie
    @23trekkie 2 ปีที่แล้ว

    According to this chrono thing, getting the 49th element of recursive Fibonacci sequence takes 149.673 seconds. That's pretty long.

  • @mr.unkreativ718
    @mr.unkreativ718 6 ปีที่แล้ว +3

    Isn't it better to use std::chrono::high_resolution_clock::time_point as type of start (6.33)?

    • @danarcese4248
      @danarcese4248 2 ปีที่แล้ว

      Thank you that quickly fixed an issue I had

  • @reedhansen5658
    @reedhansen5658 ปีที่แล้ว +2

    Suggestion: When you display names across the bottom of your screen in white, if you outline them in black it will keep them from disappearing across a light background. Then you would see the outline instead of the words blending in across something else white. Simple design trick. I really appreciate the videos. Thank you.

  • @AssemblyWizard
    @AssemblyWizard 6 ปีที่แล้ว

    Can you make a video about move semantics please? :)

  • @protasov-by
    @protasov-by 4 ปีที่แล้ว

    Hey! how about chrono based game loop mean (fps, time and delta time) delays idles etc.

  • @maxfarasat2751
    @maxfarasat2751 5 ปีที่แล้ว

    Which compiler are u using? and which C++ version

  • @yubi-152
    @yubi-152 2 ปีที่แล้ว +1

    hey, im a beginner to c++ and when I try to use the chrono, i keep getting an error that it cant be resolved. I check my version of gcc and its up to date so I have no idea what the issue it. Does anyone know what I could do here?

  • @MinhTran-wn1ri
    @MinhTran-wn1ri 4 ปีที่แล้ว

    How about a video on working with dates and time?

  • @mba2ceo
    @mba2ceo ปีที่แล้ว

    Is there way to cout the value of start & end separately ?

  • @jarisipilainen3875
    @jarisipilainen3875 5 ปีที่แล้ว +1

    8:58 got you! haha. you allways use

  • @michaelswahla4927
    @michaelswahla4927 ปีที่แล้ว +3

    You could also manually time functions in your code to see how quick they perform by passing a function pointer to the function that will time it.
    Example:
    void time_this() {
    for(int i = 0; i < 10000; i++) {
    std::cout

  • @tonychristney2728
    @tonychristney2728 4 ปีที่แล้ว +1

    Timer class should be simplified so that you use std::chrono::high_resolution_timer::time_point and std::chrono::high_resolution_timer::duration types instead of typing out the base types.

  • @sdy.deathcall9608
    @sdy.deathcall9608 ปีที่แล้ว

    I would like my while loop to cout one line every second, and not all at once

    • @SuperCoolHandle94
      @SuperCoolHandle94 ปีที่แล้ว

      Then you create the struct inside the loop at the beginning an it will destroy itself an make a new one every loop, printing the result

  • @dominiklukacs7677
    @dominiklukacs7677 4 ปีที่แล้ว +1

    Hey guys! I'm kinda new to c++, so... 8:00 what does duration.count() do?

    • @Unit_00
      @Unit_00 2 ปีที่แล้ว

      @Ludvig Sandberg that's not right. Seconds * 1000 is what gives you ms

  • @brodiequinlan1997
    @brodiequinlan1997 4 ปีที่แล้ว

    std::endl is slow because it flushes the output buffer

  • @MrCynosure4sure
    @MrCynosure4sure 5 ปีที่แล้ว

    What is the advantage of using chrono over ctime?

  • @SuperFluffyCarrot
    @SuperFluffyCarrot 6 ปีที่แล้ว

    I heard the timing is important ;)

  • @strawberriecherrie
    @strawberriecherrie 3 ปีที่แล้ว

    Why isnt end being initialised in the constructor but instead in the destructor?

  • @dx2002sg
    @dx2002sg 2 ปีที่แล้ว

    a print hello function already 70ms? wondering how then the figh freq trading firm make money? they algo runs at sub mili second.

  • @lKOKOLIZO
    @lKOKOLIZO 6 ปีที่แล้ว

    What are the main difference between C++ and Java?

  • @khatharrmalkavian3306
    @khatharrmalkavian3306 2 ปีที่แล้ว

    std::endl is slow because it flushes the stream. There was some discussion years ago about deprecating it because it's not clear about that from the name, but I think the conclusion was that nobody gives a rip about the performance of streams anyway.

  • @logangraham2956
    @logangraham2956 4 ปีที่แล้ว

    im not too interested in performance really but rather i just want to tinker around with the code , however performance is a nice side benefit i guess :)
    you'd never find me writing code for python or any of the other index block based programming languages.
    i might consider java some day but for now i would like to learn c++.
    maybe i'll try to create my muilti-tool i once had in batch script inside of c++ but then i could add so much more functionality

  • @platin2148
    @platin2148 6 ปีที่แล้ว

    Xcode does provide a profilers but they are not very usable or I'm probably to stupid to use them.

  • @abhijeetjain04
    @abhijeetjain04 6 ปีที่แล้ว

    Hey, what happened about the another thread video !!

  • @smileynetsmileynet7922
    @smileynetsmileynet7922 3 ปีที่แล้ว +1

    I've wrote loops like that before!

    • @smileynetsmileynet7922
      @smileynetsmileynet7922 3 ปีที่แล้ว +1

      Can't remember off hand without looking, but there is likely some in PNF.

  • @supremedeity9003
    @supremedeity9003 6 ปีที่แล้ว

    please do a video in how to get time

  • @daemond7746
    @daemond7746 4 ปีที่แล้ว

    will the compiler do such optimization that call an object`s deconstruct function immediatly when the object do not be used in the rest of its enclosed function,for example,the timer variable

    • @TheCherno
      @TheCherno  4 ปีที่แล้ว +1

      No, that will never happen. That's not an optimization, that would be changing the order of code execution, which of course is illegal for a compiler to do.

    • @daemond7746
      @daemond7746 4 ปีที่แล้ว

      @@TheCherno so there`s no reorder in c++?

  • @wuspoppin6564
    @wuspoppin6564 ปีที่แล้ว

    Damn the voice echo was really loud back then

  • @pierfrancescopeperoni
    @pierfrancescopeperoni 2 ปีที่แล้ว

    All this episode to say that
    is much faster than endl.

  • @shashikantverma6095
    @shashikantverma6095 4 ปีที่แล้ว

    Why my Timer constructor is shown to be +1 overloaded in visual studio with green squiggles?

  • @michaldvorak2230
    @michaldvorak2230 6 ปีที่แล้ว

    I wrote the exact same code to test it out on my laptop but I got mixed results.. I know the performance of printing out a text to the console is marginal yet I got over 100ms differences when running the code multiple times.. as well as sometimes running it with '
    ' instead of 'std::endl' didn't do any difference.. is this normal?

    • @fredhair
      @fredhair 6 ปีที่แล้ว

      Michal Dvorak endl flushes the stream buffer, i always use
      when using ostreams.

    • @michaldvorak2230
      @michaldvorak2230 6 ปีที่แล้ว

      Yeah but why does my PC show no difference in the performance?

    • @fredhair
      @fredhair 6 ปีที่แล้ว

      Michal Dvorak are you compiling with optimization flags ? If you do not use the stream much it unlikely to make much difference in performance anyway.

  • @cyberimperator
    @cyberimperator 4 ปีที่แล้ว

    Oh yeah std::chrono is really cool. Then I think about, how i measure times on a microncontroller without Timer. Set an GPIO on high before function entry and set the GPIO to LOW after the function. And Measure with with an Osciloscope. And export that measurements as an csv file, and extract the Measurement with a Python Script out of this 400 MB CSV File.

  • @VoidAshen
    @VoidAshen 3 ปีที่แล้ว

    set the font in window properties to the lowest possible,
    got 15ms lol
    also i didnt put
    just to see how low can i get and i also didnt set the compiler to prioritize speed

  • @amosreginaldjr.4200
    @amosreginaldjr.4200 ปีที่แล้ว

    The cherno?
    More like the chrono

  • @NathanKopilec
    @NathanKopilec 3 ปีที่แล้ว

    std::endl was actually faster than the escape newline
    for me.

  • @VivekYadav-ds8oz
    @VivekYadav-ds8oz 5 ปีที่แล้ว

    Don't convert durations yourself using .count(), that's not recommended. Instead, use duration_cast, much better and type safe, plus comes with no overhead (everything done at compile time).

  • @akshaynaik4888
    @akshaynaik4888 6 ปีที่แล้ว +18

    Liked before watching the video and no regrets ,😅😅

  • @MeeroSom
    @MeeroSom 3 ปีที่แล้ว +1

    I had to use decltype(std::chrono::high_resolution_clock::now()) start,end;

  • @creatork999
    @creatork999 3 ปีที่แล้ว

    Please make C tutorial :) !

  • @lc7592
    @lc7592 5 ปีที่แล้ว

    😢 I regret choosing this class. It’s soooo hard

    • @GfastGao
      @GfastGao 5 ปีที่แล้ว

      There are really many languages earsier these days. I personally see C++ as a iconic thing. It's for pretty only for love & happyness to understand a little bit more.

  • @Lightn0x
    @Lightn0x 2 ปีที่แล้ว

    I know it sounds dumb... but I've always done clock() / CLOCKS_PER_SEC :D

  • @ilyessouibgui3469
    @ilyessouibgui3469 2 ปีที่แล้ว

    Why he said he can't use the auto keyword in the Timer struct constructor