Benchmarking C# code using BenchmarkDotNet

  Рет қаралды 58,662

Nick Chapsas

Nick Chapsas

Күн бұрын

Пікірлер: 71
@RichardONeil
@RichardONeil 4 жыл бұрын
17 mins of excellent education
@kirole7381
@kirole7381 4 жыл бұрын
I managed to get some more nanoseconds for the manual conversion by just simply doing that :) int length = yearAsSpan.Length; for (int i = 0; i < length; i++) { temp = temp * 10 + (yearAsSpan[i] - '0'); }
@RetroRick1990
@RetroRick1990 3 жыл бұрын
That's great, now I have more confidence in showing off my work without having all the boring discussions about what is fast and what is not... There is always that guy who thing his code is very small and this is the way to go...
@harag9
@harag9 3 жыл бұрын
Agree, and when I saw the last change Nick did, it blew me away as it's the longest code there, and even a loop.
@subhamagarwal1716
@subhamagarwal1716 4 жыл бұрын
Stunned after seeing the results.....Its amazing.....Looking for more videos like this
@athar3353
@athar3353 3 жыл бұрын
I feel so bad that I just recently came to know about your videos. Please keep sharing your knowledge regularly. Means a lot to me.
@utubekade
@utubekade 3 жыл бұрын
One of the best programming channels on KZbin. Many useful topics for all levels of programmers. Kudos.
@millch2k8
@millch2k8 3 жыл бұрын
Great video!... I was surprised that the manual conversion (ie. the final one) was so much quicker still. I'd have naively assumed that int.Parse would have been pretty well optimized under the hood. Definitely a learning experience to see that.
@zondaken
@zondaken 2 жыл бұрын
Parsing is a complex process that needs to cover a lot of edge cases. The last benchmark uses a computation that ASSUMES that the date is in the right format. If it's not, you'll likely get problems. This is what int.Parse tries to avoid/counter.
@RobEdwards369
@RobEdwards369 2 жыл бұрын
Thanks so much for all you do. I just did my first ever benchmark. I was able to reduce my method from 87000ns/16272B to 4ns/24B. What is the best though isn't this individual case, it's being able to try various techniques and observe not just how fast/mem hungry they are but how they scale. It leads to increased understanding of performance generally so in the future I hope to know when I should say use linq or as in this case write an extra 4 lines of code and do it myself.
@JedidiJedidi
@JedidiJedidi 4 жыл бұрын
Continue the great work. I just binge-watched all your videos even the stuff that I already know.
@harag9
@harag9 3 жыл бұрын
Fairly new to your channel and was curious when you used [Benchmark] so watched this... WOW impressed with the last change you did, 4 times the length of code, and yet it ran so quick. Thanks for sharing!
@Myself0094
@Myself0094 2 жыл бұрын
Great stuff! Wrote my first benchmark today. Was very interesting to see huge difference in performance between lambda assignments and direct assignments in my own case. Though it had very small impact on a calling context execution.
@noctavel
@noctavel 4 жыл бұрын
Can't believe int.parse is slower than that last funcion :0
@geoffxander7970
@geoffxander7970 4 жыл бұрын
He went old school and banged out atoi(). I like it. But this is great info, I'll probably use this in a few months.
@millch2k8
@millch2k8 3 жыл бұрын
That definitely threw me as well.
@thiagomatu
@thiagomatu 4 жыл бұрын
wow, that's great, would be nice if you can continue with this video!
@billias28
@billias28 4 жыл бұрын
Excellent presentation of the topic. Thank you once again for your great work !
@jmangan
@jmangan 3 жыл бұрын
Great video Nick, everything I needed to get me started on this topic. Keep up the good work!
@PeterOeC
@PeterOeC 2 жыл бұрын
This video series is 🔥 bro! Thank you for making the videos! Subscribed!
@minhan4444
@minhan4444 9 ай бұрын
Thank you for video about benchmark
@dheeraj0076
@dheeraj0076 2 жыл бұрын
HI Nick, I love watching your tutorials. You are a great source of knowledge and I want to thank you for that. Just a suggestion: Would you mind keeping your fonts little bigger to ease our eyes? Thanks :)
@Emis333
@Emis333 3 жыл бұрын
Amazing work, simple and straight to the point.
@moab5102
@moab5102 3 жыл бұрын
Excellent presentation as always. watched this video few times now, I'd like to see more bench marking and optimizations videos, Thank you once again for your great work.
@MiikaKontio
@MiikaKontio 3 жыл бұрын
Very interesting topic. Thanks for introducing it to me
@Azukayoshi
@Azukayoshi 4 жыл бұрын
Amazing Job man! Thanks for the dazzling work!
@two-bitcoding8018
@two-bitcoding8018 3 жыл бұрын
Thank you for this video. Really helped me with my project!
@Victoare
@Victoare 3 жыл бұрын
Great video Nick. Binging through your uploads :) Also .. why find the hyphen at all? IndexOfHypen should always be 4. At this scale all method calls should be avoided. :D
@harag9
@harag9 3 жыл бұрын
I was wondering that, why not just get the 4 characters as you know the format and that it's the year... Now I need to copy his code and do the tests myself :)
@Fasiibcs
@Fasiibcs 4 жыл бұрын
Nice work, please make an advance video on this topic.
@mabakoo
@mabakoo 4 жыл бұрын
Excellent explanation of benchmarking. Can you help in benchmarking codes that relate to message transmission. Something like performance between signalr and rabbitmq to transmit messages
@jaysennaidoo
@jaysennaidoo 2 ай бұрын
Many years later, it seems that .NET8 optimizations for int.Parse() means that it runs faster than the manual conversion with spans
@ismailm123
@ismailm123 4 жыл бұрын
Really enjoyed this useful video, very well explained.
@davidopdebeeck8953
@davidopdebeeck8953 3 жыл бұрын
Fantastic teach dude, thanks so much!!!
@ahmeddan7734
@ahmeddan7734 4 жыл бұрын
thanks for sharing. that's is such a great tutorial and well done
@mihaimyh
@mihaimyh 3 жыл бұрын
Great video, I would love for a more in-depth one.
@ARS-fi5dp
@ARS-fi5dp 2 жыл бұрын
Thank you a lot ❤
@R.B.
@R.B. 2 жыл бұрын
I'm not completely sure why, but I was actually getting better performance passing in the string as a parameter, just like the other functions and then converting to a span within the function: ``` public int GetYearFromSpan(string dateTimeAsString) { var span = dateTimeAsString.AsSpan(); var indexOfHyphen = span.IndexOf('-'); return int.Parse(span[..indexOfHyphen]); } ``` My best performing method was this (optimizing out that the index will be 4, but the difference is nominal): ``` public int GetYearFromSpanWithManualConversionUsingForeach(string dateTimeAsString) { var yearAsSpan = dateTimeAsString.AsSpan()[..4]; var temp = 0; foreach (var c in yearAsSpan) { temp = temp * 10 + (c - '0'); } return temp; } ``` The first here, creating the Span inside the method is 0.031 (32.25 times faster) of the base, and the tightest optimization with the manual conversion is 0.007 (142.85 times faster). I was a little surprised with this outcome... (while putting this together and subsequent runs it seems that passing in the ReadOnlySpan is sometimes slightly faster, but both are within the Error). Personally it makes more sense to me to keep the signature the same if there is such an insignificant difference. I ran this against .NET 6.0.0.
@arty_3794
@arty_3794 Жыл бұрын
Thank you for video
@elmenolnecio
@elmenolnecio 2 жыл бұрын
Pretty Good man!!
@manojtayde
@manojtayde 2 жыл бұрын
Can we use BenchmarkDotNet with the NUnit framework and compare the result with the baseline result? We have CodeAnalyzer rules which are running very slow so want to check the performance. Can I achieve the same through BenchmarkDotNet ?
@tullochgorum6323
@tullochgorum6323 Жыл бұрын
Wondering why you were getting the index of the hyphen at runtime, when you know the length of the date in advance (4 chars) so it could be hard-coded. Am I missing something?
@richardc215
@richardc215 4 жыл бұрын
Good stuff. Please make an advance video on this topic.
@deanbullen9021
@deanbullen9021 2 жыл бұрын
Thanks for this, great video. So is this something you should always create a dedicated app for, or is it ok to put benchmarking inside your real business app (eg a web app)?
@anastasiiamelnyk6072
@anastasiiamelnyk6072 2 жыл бұрын
Thank you for great video! How do you think does it make sense to run Benchmark for retrieving data from database? I want to figure out what is better, run stored procedure or complicated select query.
@serhiihorun6298
@serhiihorun6298 4 жыл бұрын
Thank you
@cartman_soc
@cartman_soc 2 жыл бұрын
nice video, can you use it with xUnit ?
@zbaktube
@zbaktube Жыл бұрын
The quickest solution here is to simple return 2019. Though this solution might not be accurate in each cases🙂
@nykytakudrya7741
@nykytakudrya7741 4 жыл бұрын
thank you very much!!!!!
@imoto8709
@imoto8709 Жыл бұрын
great
@sabitkondakc9147
@sabitkondakc9147 3 жыл бұрын
thanks for this amazing video, benchmark is used by many depelopers lately it provides flexibility to analyze performance exc. but I've a problem; it takes a lot of time for large size arrays I think it's about my machine, intel i5 , 4 logical 2 physical core. does it take long to analyze with an array of [8000,8000] by your PC , which looks like a monster, 8 physical cores, damn boi ?
@cuongme626
@cuongme626 2 жыл бұрын
Great video! Can someone explain the manual conversion logic? (ie. * 10 and + ‘0’). Thanks!
@hinekde
@hinekde 2 жыл бұрын
Brackets first: Every character has a numeric value, when you look in a ASCII table for example, you see, that the character '0' is at decimal 48, '1' is 49, etc. so if you subtract '0' from a char that is between '0' and '9', you get numerical value of the displayed number. Example: ('3' - '0') = (51 - 48) = 3
@hinekde
@hinekde 2 жыл бұрын
Then the temp * 10: the loop starts at the most left character, that is the highest position of the number. So if the span is '2019', in the first iteration it reads the 2, so temp = 0 * 10 + 2 = 2 next loop it reads the 0 and adds it to the previous value times 10, so temp = 2 * 10 + 0 = 20 + 0 = 20 next loop it reads the 1 and adds it to the previous value times 10, so temp = 20 * 10 + 1 = 200 + 1 = 201 last loop it reads the 9 and adds it to the previous value times 10, so temp = 201 * 10 + 9 = 2010 + 9 = 2019
@adamowski10
@adamowski10 3 жыл бұрын
Hi, great video:) How can I mesure cpu usage with BenchmarkDotNet?
@zoltantorok1189
@zoltantorok1189 6 ай бұрын
You make the distinction between this (benchmarking) and profiling. The video is 4 years old and you have nothing on profiling. Could you make a video on profiling?
@md.mizanurrahmanmilon3745
@md.mizanurrahmanmilon3745 4 жыл бұрын
awesome
@koorshashirazi2037
@koorshashirazi2037 4 жыл бұрын
Awesome 👌👌👌
@andinofaturahman3649
@andinofaturahman3649 3 жыл бұрын
Is the result will same in xamarin ? or is benchmark valid if i run it on xamarin app ? bethink native for mobile apps is java for android.
@dotnetdevni
@dotnetdevni 2 жыл бұрын
Can you run it inside unit tests at all
@nickchapsas
@nickchapsas 2 жыл бұрын
You wouldn’t run it inside unit tests but you could have it as part of your CI pipeline and run it there
@minhgiangho1410
@minhgiangho1410 4 жыл бұрын
wow
@DungNguyen-eg7ss
@DungNguyen-eg7ss 4 жыл бұрын
nice :
@sefatanam
@sefatanam 3 жыл бұрын
Get more video
Dynamically enabling features with Feature Flags in .NET Core 3.1
11:45
What are record types in C# and how they ACTUALLY work
15:36
Nick Chapsas
Рет қаралды 120 М.
Girl, dig gently, or it will leak out soon.#funny #cute #comedy
00:17
Funny daughter's daily life
Рет қаралды 18 МЛН
Bike vs Super Bike Fast Challenge
00:30
Russo
Рет қаралды 23 МЛН
Stop Using FirstOrDefault in .NET! | Code Cop #021
12:54
Nick Chapsas
Рет қаралды 24 М.
.NET Benchmarking
27:57
Coding Tutorials
Рет қаралды 2,8 М.
Brutally honest advice for new .NET Web Developers
7:19
Ed Andersen
Рет қаралды 183 М.
8 await async mistakes that you SHOULD avoid in .NET
21:13
Nick Chapsas
Рет қаралды 312 М.
Every single feature added in C# 11
27:07
Nick Chapsas
Рет қаралды 111 М.
Intro to Benchmark.net - How To Benchmark C# Code
52:15
IAmTimCorey
Рет қаралды 40 М.
Writing C# without allocating ANY memory
19:36
Nick Chapsas
Рет қаралды 148 М.