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
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! :)
yes
same
Wow, You just sounded arrogant, then Cherno Kicked you into Humbleness.
@@ataladin87 no they didn't? they just thought they had a good understanding of c++
This series is so fucking good, keep it up
#include
#include
que es eso
@@danielesquivel3155 chrono trigger es un juego
Nice
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 ;)
Beautiful presentation. Thank you!
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.
I had same error in qmake. Thanks!
Justin Chrysler Thanks.
Justin Chrysler I want to know the reason if you know please let me know.
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.
It's the same on the gcc compilers.
excellent as always..hope u do more of these more often.
Love your videos man.
This series is really helpful and I'm looking forward to the game engine series.
std::endl is slow beacuse for each line it must flush buffer
On my pc, removing endl and adding
actually made it slower... good 50% slower. weird
@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.
@@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.
What does flushing the buffer means?
@@anthonysteinerv He's from New Zealand... they call the toilet the buffer. 👍
Awesome video! Never knew about this chrone library. Super handy! :)
i'd like to hear you talk about callbacks in programming/c++. great videos
The Timer structure is a brilliant idea. Thanks!
So great tutorial... Good luck on your way!! Thank you.
Good to see you back, Master!
Thank you, this was a big help
Cherno: You are using c++ because you obviously care about performance
ME: Nah , i just want my code to run !!!
xDdDDDD
Very interested to see your upcoming game engine series.
This helped me a lot, thanks!
Great Video Bro 👍👍
Thanks for this video
Thank you !
This video is right on time
Muito obrigado!
The best c++ chanel good job
I really liked this video .. like I pressed the Like button with full of thankfulness ... thank you so much for this explanation ❤❤
Awesome video
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 !
Great series! Consice with still being in-depth, plus functional examples.
This lessons are great thank you, when is the Game Engine series starting? cheers!!
TheChronoProject
=)
Klaus Uhr Scherzkeks
xDdDDDD
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*
Bravissimo
This could be so useful to me, there are some functions I've been working on that really need profiling.
This is genius.
Hi love your videos. Please make video on different callback methods in c++
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)..
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.
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!
Gold here!
Hero
3:23 literally, it is enlightening moment!
😳 what just happened there?
how 1s is a valid value?
#include
will do everything for you
Can u make a video about the "constexpr" stuff?I can't understand how it actualy works .Ty
what a cool ass guy
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.... :)
@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++?
cool
What's overhead? Thank you for this series btw
The endl keyword is Slow Because Is Flushes all The Output Buffer And Then Put Newline
How was GDC? anything interesting to share with us?
Woah! Wish I was there -- procedural generation is, one of my favorite topics / things to experiment with.
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 ...
Hey Cherno did you talked before about how to handle exceptions??
If you have the type error during struct, change std::chrono::time_point start, end; to std::chrono::time_point start, end;
THANK YOU
Great video but can you tell me what value does now() return
Hey! Can you do an operator overloading video
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.
For Timer struct fail to compile:
I changed 7th line, and it worked.
std::chrono::time_point start, end;
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.
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.
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.
Unless there is a specific millisecond function, then that is the way to do it.
According to this chrono thing, getting the 49th element of recursive Fibonacci sequence takes 149.673 seconds. That's pretty long.
Isn't it better to use std::chrono::high_resolution_clock::time_point as type of start (6.33)?
Thank you that quickly fixed an issue I had
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.
Can you make a video about move semantics please? :)
Hey! how about chrono based game loop mean (fps, time and delta time) delays idles etc.
Which compiler are u using? and which C++ version
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?
How about a video on working with dates and time?
Is there way to cout the value of start & end separately ?
8:58 got you! haha. you allways use
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
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.
I would like my while loop to cout one line every second, and not all at once
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
Hey guys! I'm kinda new to c++, so... 8:00 what does duration.count() do?
@Ludvig Sandberg that's not right. Seconds * 1000 is what gives you ms
std::endl is slow because it flushes the output buffer
What is the advantage of using chrono over ctime?
I heard the timing is important ;)
Why isnt end being initialised in the constructor but instead in the destructor?
a print hello function already 70ms? wondering how then the figh freq trading firm make money? they algo runs at sub mili second.
What are the main difference between C++ and Java?
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.
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
Xcode does provide a profilers but they are not very usable or I'm probably to stupid to use them.
Hey, what happened about the another thread video !!
I've wrote loops like that before!
Can't remember off hand without looking, but there is likely some in PNF.
please do a video in how to get time
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
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.
@@TheCherno so there`s no reorder in c++?
Damn the voice echo was really loud back then
All this episode to say that
is much faster than endl.
Why my Timer constructor is shown to be +1 overloaded in visual studio with green squiggles?
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?
Michal Dvorak endl flushes the stream buffer, i always use
when using ostreams.
Yeah but why does my PC show no difference in the performance?
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.
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.
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
The cherno?
More like the chrono
std::endl was actually faster than the escape newline
for me.
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).
Liked before watching the video and no regrets ,😅😅
Same as always
me 2 lel
I had to use decltype(std::chrono::high_resolution_clock::now()) start,end;
Please make C tutorial :) !
😢 I regret choosing this class. It’s soooo hard
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.
I know it sounds dumb... but I've always done clock() / CLOCKS_PER_SEC :D
Why he said he can't use the auto keyword in the Timer struct constructor