Timing in C++

แชร์
ฝัง
  • เผยแพร่เมื่อ 26 ม.ค. 2025

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

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

    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 4 ปีที่แล้ว +1

      yes

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

      same

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

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

    • @joedartonthefenderbass
      @joedartonthefenderbass ปีที่แล้ว +8

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

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

    This series is so fucking good, keep it up

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

    #include
    #include

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

    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.

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

    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 ;)

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

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

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

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

    • @them4309
      @them4309 6 ปีที่แล้ว +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 4 ปีที่แล้ว

      @@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 3 ปีที่แล้ว

      What does flushing the buffer means?

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

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

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

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

  • @thesupremepebble3690
    @thesupremepebble3690 6 หลายเดือนก่อน +1

    i felt so smart when i thought of that neat little trick with the timer class before you explained it

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

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

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

    Very interested to see your upcoming game engine series.

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

    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 5 ปีที่แล้ว +1

      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 ปีที่แล้ว +1

      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!

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

    The best c++ chanel good job

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

    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*

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

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

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

    Love your videos man.

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

    Great Video Bro 👍👍

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

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

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

    3:23 literally, it is enlightening moment!

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

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

  • @pranalisuhaspatil
    @pranalisuhaspatil 4 หลายเดือนก่อน

    You are so detailed in short videos
    Please make one playlist on python also

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

    Good to see you back, Master!

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

    Beautiful presentation. Thank you!

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

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

  • @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

  • @Finger152
    @Finger152 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?

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

    The Timer structure is a brilliant idea. Thanks!

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

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

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

    #include
    will do everything for you

  • @gordonmccrary6279
    @gordonmccrary6279 5 หลายเดือนก่อน

    funny side note: I did two functions one with printf and one with cout and the old school c printf was a third of the time

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

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

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

    This helped me a lot, thanks!

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

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

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

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

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

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

    Thank you, this was a big help

  • @talha_coding_tutor
    @talha_coding_tutor 7 หลายเดือนก่อน

    Yan Chernikov ! My C++ Mentor !!
    I Learned And Mastered Pointers (* = raw) with your help because these stars thing are too annoying.
    Love From Pakistan

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

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

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

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

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

    Hey! Can you do an operator overloading video

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

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

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

    Awesome video

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

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

  • @michaelswahla4927
    @michaelswahla4927 2 ปีที่แล้ว +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

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

    TheChronoProject
    =)

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

    This is genius.

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

    Thanks for this video

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

  • @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 3 ปีที่แล้ว

      Thank you that quickly fixed an issue I had

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

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

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

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

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

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

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

    Muito obrigado!

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

    This video is right on time

  • @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 !

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

    Thank you !

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

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

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

    Why is it taking 77 ms? In my system it completes in 0.8 to 1 ms.

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

    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.

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

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

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

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

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

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

    How about a video on working with dates and time?

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

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

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

      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 5 ปีที่แล้ว +1

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

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

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

  • @Davi-c4q
    @Davi-c4q 6 ปีที่แล้ว

    7:50 line 21 why

    • @Davi-c4q
      @Davi-c4q 6 ปีที่แล้ว

      using duration.count() works

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

      You cannot put the chrono object into the standart stream, you need to convert it to a float/double/int as Cherno does a couple of seconds later.. if you wanted to print out the 'duration' variable, you'd need to print duration.count()

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

      if you wanted to print just 'duration' you'd need to overload the ostream operator for it to handle it

  • @MostEpic_Channel
    @MostEpic_Channel 14 วันที่ผ่านมา

    What is that faking tomato in auto complete?

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

    Hey, what happened about the another thread video !!

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

    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

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

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

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

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

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

  • @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 3 ปีที่แล้ว

      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.

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

    interesting. have they changed std::endl?
    Timer took 23.6653ms with std::endl;
    Timer took 27.7078ms with

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

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

    Gold here!

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

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

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

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

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

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

    hi ! i get "error: no match for ‘operator=’ (operand types are ‘std::chrono::time_point’ and ‘std::chrono::_V2::system_clock::time_point’ {aka ‘std::chrono::time_point’})
    16 | start = std::chrono::high_resolution_clock::now();" error
    std:chrono::time_point Changing this part to std::chrono::system_clock::time_point solved the problem. I don't understand
    Please. could you explain it?

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

    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

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

    Whats wrong with clock_t time = clock(); ?

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

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

    Call me crazy but; removing endl is useless if you want speed... Why not use printf(); ? Looks like C++ folks always forget C exists and their headers are there. #include not #include

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

    What's overhead? Thank you for this series btw

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

    What is the advantage of using chrono over ctime?

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

    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  5 ปีที่แล้ว +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 5 ปีที่แล้ว

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

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

    please do a video in how to get time

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

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

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

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

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

    Damn the voice echo was really loud back then

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

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

  • @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++?

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

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

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

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

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

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

  • @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 3 ปีที่แล้ว

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

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

    This is does not seem to be working on linux, getting tons of errors of missing operator overloads and so on, using gcc.

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

      Is your target C++ standard correct?
      If not then you should pass -std=c++11 (or higher) to the compiler

    • @HydeFromT70s
      @HydeFromT70s 10 หลายเดือนก่อน

      @@groberti He should pass -std=c++2a

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

    what a cool ass guy

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

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

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

    Bravissimo

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

    Why does he write "std::" in every statement instead of putting "using namespace std" at the top?
    Very useful video btw

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

      There's a whole video about that.