this is why your code is slow...

Поділитися
Вставка
  • Опубліковано 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
  • Наука та технологія

КОМЕНТАРІ • 243

  • @eugeneyap4208
    @eugeneyap4208 Рік тому +8

    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

  • @donizetevida2149
    @donizetevida2149 Рік тому +4

    *final* keyword on class is about inheritance not about immutable instance.
    Long should be on stack without a *new* keyword, like in C++.

  • @cesarvalencia6191
    @cesarvalencia6191 Рік тому +10

    As always, very educational and I always learn something new from your videos. a demo on Profiling would be great.

  • @trustDproc3ss
    @trustDproc3ss Рік тому +14

    Please make a video on profiling, it would be great adding that tool in my development. Thank you for this.

  • @nandorholozsnyak7799
    @nandorholozsnyak7799 Рік тому +10

    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.

  • @gulbalasalamov1367
    @gulbalasalamov1367 Рік тому +54

    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

    • @shadamethyst1258
      @shadamethyst1258 Рік тому

      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

  • @aminesafi7261
    @aminesafi7261 Рік тому +4

    Excellent video, you explain things in a very simple way.
    Many thanks and keep going 💪🏻

  • @TischTiger
    @TischTiger Рік тому +27

    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.

    • @mrtnsnp
      @mrtnsnp Рік тому +4

      In LLVM this is actually what the optimiser does (in O3). Of course not in Java, but plenty of other languages to choose from.

    • @corejake
      @corejake Рік тому

      My first thought was the same, except just doing it in calc and then replacing the function with a constant result.

    • @mrtnsnp
      @mrtnsnp Рік тому

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

    • @kenzostaelens1688
      @kenzostaelens1688 Рік тому

      fun fact, (Integer.MAX_VALUE+1)/2 is exactly 2^32 so you could even shift Integer.MAX_VALUE by 32

  • @mostafamabrouk8806
    @mostafamabrouk8806 Рік тому +4

    The profiler part was very good as I wanted something like that to keep track of performance. ❤

  • @damianjoel5833
    @damianjoel5833 Рік тому +1

    You are the best bro, thanks a lot for all your videos!

  • @miloplayz5264
    @miloplayz5264 Рік тому

    Salam AmigosCode, Thanks for all your content you are doing! I wish you a very well Ramadan month!

  • @tomaszvinys2389
    @tomaszvinys2389 Рік тому

    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.

  • @amirulidzham
    @amirulidzham Рік тому

    Thanks brother for the tips. Ramdan Kareem brother, happy fasting...

  • @alexis9314
    @alexis9314 Рік тому

    Thanks for showing the improvement and how to work it!

  • @furkan8049
    @furkan8049 Рік тому

    Thank you for the information. Good job.

  • @dngrz
    @dngrz Рік тому

    Excelent! I Will try! Good improvement ! Thank You a Lot 👍

  • @creepymuffin2351
    @creepymuffin2351 Рік тому

    Thanks, that is a really good advice!

  • @sweetphilly2
    @sweetphilly2 Рік тому

    Damn that was informative. That illustration of call and memory stack and heap is clean af too - super easy to understand

  • @xxkelel20xx
    @xxkelel20xx Рік тому

    I learn something new everyday in my programming journey!

  • @user-qm7gy4oo3k
    @user-qm7gy4oo3k Рік тому

    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)

  • @techspeechplus582
    @techspeechplus582 Рік тому

    Thank you for the explanation and it was helpful

  • @pradeepkumardodda6109
    @pradeepkumardodda6109 Рік тому

    Thank you for your efforts, I request you to do more videos like this, which helps to improve the code

  • @som3ah0
    @som3ah0 Рік тому

    Thank AmigosCode for demonstrating the improvement using profiler.... jazak allah khair ... please we want more about profiler

  • @raymondsam5642
    @raymondsam5642 Рік тому +1

    Thank you for the information. Good job.
    A tutorial on profiling would be fantastic.

  • @rajsundarlogasundaram1596
    @rajsundarlogasundaram1596 Рік тому

    Meticulous catch here in our day to today code block... Kudos dude

  • @sharadsingh2856
    @sharadsingh2856 Рік тому

    Great learning thanks

  • @andrejtrozic509
    @andrejtrozic509 Рік тому

    Best Java programming teacher on UA-cam.

  • @Mac1355
    @Mac1355 Рік тому

    tks for the explanation

  • @kchemutai3483
    @kchemutai3483 Рік тому

    Very informative. Thanks amigos code

  • @rajkhare5949
    @rajkhare5949 Рік тому +2

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

  • @daichiakiba5701
    @daichiakiba5701 Рік тому

    It's very helpful! Which profiler tool are you using?

  • @UberDurable
    @UberDurable Рік тому

    Very useful, thanks!

  • @eduardomatos666
    @eduardomatos666 Рік тому +2

    Awesome!! Please create more videos like that 😍😍

  • @Yotanido
    @Yotanido Рік тому

    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.

  • @networkingknowledge6261
    @networkingknowledge6261 Рік тому

    thx for the great tip , we want pliz a full video pliz !! , thx a lot

  • @nurmuhammadikhsan5876
    @nurmuhammadikhsan5876 Рік тому

    THANKS, its helpful

  • @jihedbouchnak
    @jihedbouchnak Рік тому

    i will love to see more video a bout clean code an optimisations good job

  • @sadeepaalwis5938
    @sadeepaalwis5938 Рік тому +2

    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

  • @ricksanchez3873
    @ricksanchez3873 Рік тому

    Thx for the video, sir.

  • @imblackmagic1209
    @imblackmagic1209 Рік тому +1

    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

  • @ahmede7141
    @ahmede7141 Рік тому

    Thanks, and please do more of this type of content please

  • @Sasikumar-kr7xy
    @Sasikumar-kr7xy Рік тому +1

    Thanks for the demo on performance using profiler. Please do a dedicated on profiler

  • @dmytro4312
    @dmytro4312 Рік тому

    Hi, yes, it will be very interesting to hear about profiler

  • @alexandresantos7966
    @alexandresantos7966 Рік тому +1

    What situation do you think that's better to use wrapper class?

  • @user-dp9uv9rs8x
    @user-dp9uv9rs8x Рік тому

    thanks for a video! nice:) I`d like to see more about profiling

  • @justindouglas3659
    @justindouglas3659 Рік тому +1

    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

  • @FIXXX777
    @FIXXX777 Рік тому

    Hi bro Ramadan Mubarak 🤗 thanks for content

  • @leeamraa
    @leeamraa Рік тому

    great brother. walaikum alsalam warahmatu allah

  • @amarthyaseshu683
    @amarthyaseshu683 Рік тому

    Thanks for sharing

  • @MrJgremmen
    @MrJgremmen 8 місяців тому

    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.

  • @deorgeneslorddj
    @deorgeneslorddj Рік тому +1

    Please make a video about profiling. It was incridible!

  • @gleitonfranco1260
    @gleitonfranco1260 Рік тому

    Awesome tool! 😲

  • @SuperKidontheblock
    @SuperKidontheblock Рік тому

    Profiles are very underused. Advocating for more people to use them especially when creating webserbices

  • @mahmoudchebbani4807
    @mahmoudchebbani4807 Рік тому

    A video on profiling would be amazing!

  • @Speiger
    @Speiger Рік тому +3

    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)

  • @tairovich.solutions
    @tairovich.solutions 10 місяців тому

    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

  • @gawwad4073
    @gawwad4073 Рік тому +2

    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.

  • @narongsakyooyen9572
    @narongsakyooyen9572 Рік тому

    Great content mate 😂

  • @Alptugaydin
    @Alptugaydin Рік тому +1

    That was definitely impressive. I would like to see more videos, especially involving resource optimization, and profiler. You are doing great, thanks

  • @lejeq
    @lejeq Рік тому

    Really good video!

  • @Synkto01
    @Synkto01 Рік тому

    Amazing, yes pls teach us about profiling and how to undertanding

  • @nikolanaydenov2157
    @nikolanaydenov2157 Рік тому +3

    Can you please explain when we should use the wrapper classes instead of the primitive data types?

    • @Misfiring89
      @Misfiring89 Рік тому

      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

  • @doven_2750
    @doven_2750 Рік тому +55

    Hello, AmigosCode. Can you please record a video about when should we use wrapper types instead of primitives? Thank you for the video :)

    • @cloudstackz
      @cloudstackz Рік тому +13

      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.

    • @MrKozeljko
      @MrKozeljko Рік тому +1

      @@cloudstackz And at least part is meant to be solved with Project Valhalla.

    • @HoD999x
      @HoD999x Рік тому +3

      the question is not "when should you" but "when do you have to"

    • @CottidaeSEA
      @CottidaeSEA Рік тому +1

      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.

    • @CesarScur
      @CesarScur Рік тому

      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.

  • @Vladimir_Java_dev
    @Vladimir_Java_dev Рік тому

    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.

  • @vivekbansal7207
    @vivekbansal7207 Рік тому

    great video keep making content like these

  • @gasparcalix2095
    @gasparcalix2095 Рік тому

    grate content! as always

  • @Starchaser38
    @Starchaser38 Рік тому +3

    So, since Long is immutable, every time "sum += i" is invoked it re-creates the object anew??? Oh God... 😅

    • @Erlisch1337
      @Erlisch1337 Рік тому +2

      wait until you hear of strings....

  • @salihiyassine2355
    @salihiyassine2355 Рік тому

    thank you so much, yes could u make another video about profiler?

  • @zhandosyernazarov3086
    @zhandosyernazarov3086 Рік тому

    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.

  • @donatogiuliano
    @donatogiuliano Рік тому

    what a great video!

  • @Nezar-hb6vu
    @Nezar-hb6vu Рік тому

    Great stuff, by the way, are you gonna start a Domain Driven Design tutorial any soon

  • @ankitkadam8161
    @ankitkadam8161 Рік тому +4

    Which plugin are you using in intellij for checking memory graphs?

    • @MrJgremmen
      @MrJgremmen 8 місяців тому

      The IntelliJ Profiler is (as the name sort of gives it away) part of IntelliJ.

  • @dcorralf
    @dcorralf Рік тому +1

    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.

  • @semilife
    @semilife Рік тому

    Thanks once again. A video on profiling would be nice, I am new to IntelliJ coming from eclipse.

  • @jonathan.m.phillips
    @jonathan.m.phillips Рік тому

    This was a great video.

  • @jatindersinghaujla
    @jatindersinghaujla Рік тому

    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.

  • @lighttheoryllc4337
    @lighttheoryllc4337 Рік тому +1

    😂 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!

  • @jmajid774
    @jmajid774 Рік тому

    Thanks

  • @KAFJHON
    @KAFJHON Рік тому +1

    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

  • @aliakseydruzik7433
    @aliakseydruzik7433 Рік тому

    Also is important to understand how static-keyword works.

  • @nightfox6738
    @nightfox6738 Рік тому +1

    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?"

    • @petitmoi735
      @petitmoi735 Рік тому

      what do you mean by that ?

    • @nightfox6738
      @nightfox6738 Рік тому

      @@petitmoi735 I thought the comment was pretty straightforward what part didn't you understand?

    • @matheusjahnke8643
      @matheusjahnke8643 Рік тому +3

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

    • @matheusjahnke8643
      @matheusjahnke8643 Рік тому

      I mean, this optimization turns O(n) into O(1).
      But you should be careful to check if the result doesn't overflow.

    • @nightfox6738
      @nightfox6738 Рік тому +1

      @@matheusjahnke8643 absolutely, but if the result overflows in the optimization, it overflows on the original too.

  • @gregderus9042
    @gregderus9042 Рік тому

    Thanks, please more about profiler

  • @al-aminhamadi9451
    @al-aminhamadi9451 Рік тому

    The profile is great.

  • @azharponani
    @azharponani Рік тому

    What is the usecase of using long and Long..how can we decide when to use which

  • @lanceareadbhar
    @lanceareadbhar Рік тому +7

    I first read this as this is why you code slow?

  • @rusty_screw
    @rusty_screw Рік тому

    Hi, when you mentioned that class Long is final you confused immutability with "finality"(ability to extend a class)

  • @shashishekhar----
    @shashishekhar---- Рік тому

    Wa alaikum as salaam my friend,you are a gem 💎

  • @pollafattah7062
    @pollafattah7062 Рік тому

    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.

  • @filipstojiljkovic4711
    @filipstojiljkovic4711 Рік тому

    God bless Amigo

  • @bernizhel
    @bernizhel Рік тому

    I would like to see how to profile java programs, thanks for the video!

  • @ajayk2175
    @ajayk2175 Рік тому

    Very good buddy. Video on profile will be helpful

  • @CottidaeSEA
    @CottidaeSEA Рік тому

    This is why pure functions can be extremely costly as well. Know when and when not to use immutable data.

  • @user-ns6yl9gr1m
    @user-ns6yl9gr1m 4 місяці тому

    I think ,this Long type can replace to long , it's beautiful transalte

  • @dmitrik4610
    @dmitrik4610 Рік тому

    Hi. you also can use object of AtomicLong class.

  • @olaitanhazeem3401
    @olaitanhazeem3401 Рік тому

    Wa alaykum salam waramatullah wabarakatu
    Ramadan Mubarak ahkee may Allah accept it from us

  • @fahimhoque7292
    @fahimhoque7292 Рік тому +4

    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

  • @HoliakaDmytro
    @HoliakaDmytro Рік тому

    What theme was used in Intellij Idea in this video, isn't it new UI?

  • @user-oz9lw5rr6u
    @user-oz9lw5rr6u Рік тому

    Hi,Amigoscode,thank you for the video, I am very interested in how to use profile tool

  • @kishordige9721
    @kishordige9721 Рік тому

    nice one

  • @aspergerhyperactive
    @aspergerhyperactive Рік тому

    how to design a programming language and which programming language should be used to design a programming language