this is why your code is slow...
HTML-код
- Опубликовано: 28 мар 2023
- In this video I show why your code can be slow and how to improve it. When writing code to production you need be careful.
Don't Forget to
===========================================
💯 Subscribe to Amigoscode - bit.ly/2HpF5V8
💯 Courses Available for free here - amigoscode.com/courses
💯 Join Private Facebook Group and Discord - amigoscode.com/p/join-community
⭐ Table Of Contents ⭐
===========================================
🙊 Here are the goods for all my videos video 🙊
► Recommended Books
===========================================
- Clean Code - amzn.to/2UGDPlX
- HTTP: The Definitive Guide - amzn.to/2JDVi8s
- Clean Architecture - amzn.to/2xOBNXW
► Computer and Monitor
===========================================
- New Apple MacBook Pro - amzn.to/3464Mmn
- Dell 27 INCH Ultrasharp U2719D Monitor - amzn.to/2xM3nW1
- Double Arm Stand Desk Mount - amzn.to/3aYKKfs
- USB C Hub Multiport Adapter - amzn.to/2Jz7NlL
► Camera Gear
=============================================
- Sony ILCE7M3B Full Frame Mirrorless Camera - amzn.to/346QIJn
- Sigma 16 mm F1.4 DC DN - amzn.to/2wbic3Q
- Sigma 33B965 30 mm F1.4 DC DC - amzn.to/39G37Fd
► IDE & Tools I use for coding 💻 🎒
===========================================
- ITerm
- VsCode
- GoLand
- IntelliJ Ultimate
- Sublime
P.S
===========================================
💯 Don't forget to subscribe | bit.ly/2HpF5V8
💯 Join Private Facebook Group and Discord - amigoscode.com/p/join-community
💯 Follow me on Instagram | bit.ly/2TSkA9w
❤️ Thanks for watching - Наука
love the profiler demonstration to show the memory and cpu performance when running code. It really helps in visualization of how bad code vs good code perform
*final* keyword on class is about inheritance not about immutable instance.
Long should be on stack without a *new* keyword, like in C++.
As always, very educational and I always learn something new from your videos. a demo on Profiling would be great.
Please make a video on profiling, it would be great adding that tool in my development. Thank you for this.
Cool video, just a few notes:
- I'm curious why is the method in the controller with the GetMapping is private? Are you trying to make sure that even if somebody would inject the controller class to another class they won't be able to call it? If yes interesting approach.
- The Long class is not immutable because it has the "final" keyword in the class definition, it is an immutable class because the value it stores is marked as final. Code from the Long class: `private final long value;` - If it is final you can not change it after you initialized it, it can be a bit misleading.
- GC will kick in when it has to, clearly the code with the boxing is inefficient but the GC will know when it should kick in and it will eliminate the unreferenced objects from the heap.
- Using non-primitive types are having their own use cases, they can be null, so they can be representing missing or unset values, primitives can too, but it would require business modelling decisions, about which value will represent that the value is missing or not specified. (-1, 0?)
- I'm really missing "proper" explanation about what is happening in the background, it is an important topic, but I just see a profiler, but where is the decompiled byte code or even the byte code itself? If anybody want to see the stuff that is happening in the background the "javap" tool must have been mentioned.
It can describe the class file with all the byte code instructions. In the boxed version we will see virtual and static method invocations, where the boxed value is going to be converted to pure primitive type (longValue()), we will see that the integer is being casted to a long value and the result of the addition will be converted to Long with the valueOf static method. This is where the "magic" is happening, Java devs should be aware of this tool and should be using for these kind of purposes. With the primitive example there is no static or virtual method calls, only good old additions and store functions.
- I'm not against Postman but IDEA is having a cool HTTP Client, that can generate HTTP (or even gRPC) requests and it can run them, if you need a tool to quickly execute HTTP requests I would recommend to try it out, such a cool feature.
Thank you for demonstrating the improvement with graphs using profiler. That's pretty much more educational with visuals. I'm definitely going to use profiler tool too
You need a good profiler to find the slow areas of code and you need benchmarks to confirm that what you are doing is actually improving things
Excellent video, you explain things in a very simple way.
Many thanks and keep going 💪🏻
My first thought, not realizing which programming language this is, was that a loop is used here, although there is a mathematical solution to the problem that does not require a loop. So my first optimization would be to replace the loop with "Long sum = Integer.MAX_VALUE * (Integer.MAX_VALUE + 1) / 2;". But using objects where primitive data types are sufficient is also a good point.
In LLVM this is actually what the optimiser does (in O3). Of course not in Java, but plenty of other languages to choose from.
My first thought was the same, except just doing it in calc and then replacing the function with a constant result.
@@corejake The funny part is that llvm will optimize the code to the equivalent of (n-1)*n/2, so it does not pre-calculate the answer, but replace the loop with the correct mathematical expression. At first I thought it worked out a hard coded answer, so I compiled a program that takes n from the command-line.
fun fact, (Integer.MAX_VALUE+1)/2 is exactly 2^32 so you could even shift Integer.MAX_VALUE by 32
The profiler part was very good as I wanted something like that to keep track of performance. ❤
You are the best bro, thanks a lot for all your videos!
Salam AmigosCode, Thanks for all your content you are doing! I wish you a very well Ramadan month!
Man, Thank You so much for showing this! I'm missing for java such java performance tutorials a lot. I'd love to see more of videos like this one.
Thanks brother for the tips. Ramdan Kareem brother, happy fasting...
Thanks for showing the improvement and how to work it!
Thank you for the information. Good job.
Excelent! I Will try! Good improvement ! Thank You a Lot 👍
Damn that was informative. That illustration of call and memory stack and heap is clean af too - super easy to understand
Thanks, that is a really good advice!
Thx, it's important to know. I didn't think about this before watching this video. Your's videos helping me know more about Java. Make more like that)
I learn something new everyday in my programming journey!
Thank you for the explanation and it was helpful
Thank you for your efforts, I request you to do more videos like this, which helps to improve the code
Thank AmigosCode for demonstrating the improvement using profiler.... jazak allah khair ... please we want more about profiler
Thank you for the information. Good job.
A tutorial on profiling would be fantastic.
Meticulous catch here in our day to today code block... Kudos dude
Great learning thanks
Best Java programming teacher on RUclips.
tks for the explanation
Very informative. Thanks amigos code
very good video...got to know the difference between the wrapper and primitive use in real-time...please make the video on the profiler too so we can understand how to use this tool for our code performance improvement!!!
It's very helpful! Which profiler tool are you using?
Very useful, thanks!
Awesome!! Please create more videos like that 😍😍
Something I found very interesting: I used a very similar piece of code to do some performance testing in Rust.
Turns out, the compiler can actually optimise away the entire loop and just replace it with a maths expression.
Like, I knew how to do it myself, but that the compiler could just do that blew my mind.
thx for the great tip , we want pliz a full video pliz !! , thx a lot
THANKS, its helpful
i will love to see more video a bout clean code an optimisations good job
Thank you for the awesome video. please consider making a video about profiling also does it have a specific IntelliJ version to get the profiler. i checked on mine but that option wasn't available
Thx for the video, sir.
thanks, this also applies to other languages, it's always better to use the basic types
that aside, the example can be done in O(1) as there's a formula for the sum of the first n numbers, but i recognize that's not the point of the video
Thanks, and please do more of this type of content please
Hi, yes, it will be very interesting to hear about profiler
Thanks for the demo on performance using profiler. Please do a dedicated on profiler
thanks for a video! nice:) I`d like to see more about profiling
What situation do you think that's better to use wrapper class?
Yes bro you should make a vid on profiling and i hope one day junit cuz i don't know a thing about it or maybe security tools tjat would be nice
Hi bro Ramadan Mubarak 🤗 thanks for content
great brother. walaikum alsalam warahmatu allah
Thanks for sharing
Nice! I understand the code is just an example for demonstration purposes. The biggest takeaway is to be aware that primitives are faster than their boxed representation and creating massive amounts of objects on the heap within loops is a no-go. Someone already mentioned that this particular problem could be solved mathematically. It is worth mentioning something like this because there are lazy developers out there running loops or producing lots of code for things that can be done faster/easier. To even optimize the example code further, I would change the loop variable from long to int. I'm not sure if it will be noticeable in the end but it is cleaner and requires less memory accesses.
Please make a video about profiling. It was incridible!
Awesome tool! 😲
Profiles are very underused. Advocating for more people to use them especially when creating webserbices
Brother, if you don't mind me asking, what application do you use to record your screen and yourself? Also how do you make that circle video of yourself? Thank you
Just found this channel. Looked at the performance improvement you did, looked solid from experience.
Look at your other videos suggesting STREAMS over FOR LOOPS and thought, lets hope he makes a video where he redacts that statement since for loops are way faster then streams since streams build a entire state machine instead of simple iterators xD
(Note: I am writing performance collection libaries xD)
A video on profiling would be amazing!
Using primitives is a good idea in Java unless you have a reason to use the wrappers. In real programs you're not going to see performance differences like this though. Testing busy loops is just silly.
Also the reason GC left that memory uncollected is to save resources. No point running cleanup if there is plenty of space left.
Great content mate 😂
That was definitely impressive. I would like to see more videos, especially involving resource optimization, and profiler. You are doing great, thanks
Really good video!
Amazing, yes pls teach us about profiling and how to undertanding
Can you please explain when we should use the wrapper classes instead of the primitive data types?
Always use primitive types when possible, but there'll be scenarios where wrapper classes are needed:
1. Collections like lists, sets and maps. They require objects and thus you have to use wrapper classes.
2. You need null values which are not representable with 0, for example as a return type.
3. Accurate representation of decimals, e.g. Big Decimal
Hello, AmigosCode. Can you please record a video about when should we use wrapper types instead of primitives? Thank you for the video :)
There isn't much that can be said here, but generally speaking, wrapper types are typically used when working with generic types or collections where you need to use null to represent the absence of a value.
@@cloudstackz And at least part is meant to be solved with Project Valhalla.
the question is not "when should you" but "when do you have to"
Optional works. Optional doesn't.
If you then need to use the Long in a similar way to the video, you unwrap it to use a primitive.
In Martin Fowler's book, refactoring, he points out that you shouldn't prefer primitives unless it makes sense for the business. Using many primitives are usually a smell. It is recommend that you ponder about it and then replace with objects that reflect the business better.
These types of simplifications that deal with abstract code is usually bad. In reality we don't see code like this very often. Instead we look clients or items in a order and so on. The size is very different and the gains vs sacrifice made is not worth. What is the point in making a 100ms request faster by 0,001ms?
I recommend reading the first 2 chapters of refactoring.
As I understand, we have to use 'wrapper' classes only for cases where we use generics such as collection. In other cases it is more preferable to use primitives as is.
great video keep making content like these
grate content! as always
So, since Long is immutable, every time "sum += i" is invoked it re-creates the object anew??? Oh God... 😅
wait until you hear of strings....
thank you so much, yes could u make another video about profiler?
Assalamu alaikum, thank you for the video, this was something new for me. Even though I've been using Intellij for a long time, I did not know about profiler.
what a great video!
Great stuff, by the way, are you gonna start a Domain Driven Design tutorial any soon
Which plugin are you using in intellij for checking memory graphs?
The IntelliJ Profiler is (as the name sort of gives it away) part of IntelliJ.
Hi, thanks for the content, it's a great video about performance. For my, maybe it will be interesting to make more videos about applications performance and the profile option of IntelliJ.
Thanks once again. A video on profiling would be nice, I am new to IntelliJ coming from eclipse.
This was a great video.
Then where we should use primitive and warapper classes.
Primitives I can say to use when doing some calculation in a loop. But when to use wrapper classes.
😂 I don't need no hook on this beat, all I need is the CODE in the background, my headphone high, and da connect on da mic Yep!
Thanks
HI AmigosCode, I have been binge watching your tutorial videos for the last week or so, and I was wondering why you used wrapper classes instead of primitives on your tutorials, I'm a bit confused about this one, if you could please explain further when to use one or another, or is it a personal preference.
Thank you in advance
excelent video as always
Also is important to understand how static-keyword works.
What is the usecase of using long and Long..how can we decide when to use which
Reducing memory allocations is definitely a huge one but I think the most important thing to minimize is branches. Particularly loops. In this example for instance, you could use gauss's equation instead of a loop altogether ( n(n+1)/2 ) and while this is a very specific example, its important whenever you're using a loop to think "Do I really need a loop here?"
what do you mean by that ?
@@petitmoi735 I thought the comment was pretty straightforward what part didn't you understand?
@@petitmoi735 instead of calculating
S=0+1+2+3+...+n
With n additions
You do
S=n(n+1)/2
The legend says that a teacher didn't want to deal with children... so he told them to sum 1 to 100...
Unfortunately for him, little Gauss was in class... and knew the formula instead of brute forcing the result...
I mean, this optimization turns O(n) into O(1).
But you should be careful to check if the result doesn't overflow.
@@matheusjahnke8643 absolutely, but if the result overflows in the optimization, it overflows on the original too.
The profile is great.
Thanks, please more about profiler
I first read this as this is why you code slow?
Hi, when you mentioned that class Long is final you confused immutability with "finality"(ability to extend a class)
Wa alaikum as salaam my friend,you are a gem 💎
Yes Please have a video about profiling, actually Can you please have a video series about the intellij and all interesting features. I will show them to my studens.
God bless Amigo
I would like to see how to profile java programs, thanks for the video!
Very good buddy. Video on profile will be helpful
This is why pure functions can be extremely costly as well. Know when and when not to use immutable data.
Hi. you also can use object of AtomicLong class.
I think ,this Long type can replace to long , it's beautiful transalte
Wa alaykum salam waramatullah wabarakatu
Ramadan Mubarak ahkee may Allah accept it from us
Hey already posted this idea on discord but can you do a video on securing spring boot application with firebase or Auth0?
Securing a service is crucial and rather than investing time on auth manually and test the mechanism I think it’s better to depend on proven platforms to secure the applications in some cases
What theme was used in Intellij Idea in this video, isn't it new UI?
Hi,Amigoscode,thank you for the video, I am very interested in how to use profile tool
how to design a programming language and which programming language should be used to design a programming language
nice one