Big O myths busted! (Time complexity is complicated)

Поделиться
HTML-код
  • Опубликовано: 2 июн 2024
  • Is O(log n) better than O(n)? In this video we talk about algorithms, time complexity, and why it's sometimes confusing.
    Buy 🦀 Rust stickers: strager.net/booty
    00:00 what is the best algorithm?
    00:16 locating errors
    01:28 benchmarking the naïve algorithm
    02:08 analyzing the naïve algorithm
    04:33 the line tables algorithm
    05:44 benchmarking the line tables
    08:06 a fair comparison
    09:48 binary search
    10:43 analyzing binary search
    12:49 benchmarking binary search
    14:28 coding SIMD
    18:34 pop quiz answers
    Thanks:
    Jennipuff: / jenipuff
    Attribution:
    Thumbnail artwork and photography by Jennipuff
    Sound effects: pixabay.com/sound-effects/sfx... pixabay.com/sound-effects/car...
  • НаукаНаука

Комментарии • 644

  • @strager_
    @strager_  Год назад +80

    Source code of all of the techniques (for educational purposes):
    github.com/strager/big-oh-lines
    Corrections:
    19:23 I show vec![0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 12, 12, 12, ...], But this is incorrect. The Vec items should be line numbers, not offsets. (The keys are the offsets!) So the Vec would actually look like this: vec![1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, ...]. (Thanks to @polyfoxgames9006 for pointing this out.)
    Pop quizzes:
    04:24 average time complexity? answer at 18:34
    09:42 another way to preprocess? answer at 19:09
    12:23 why does binary search 2x? answer at 20:14
    14:17 time complexity of SIMD algo? answer at 20:47
    Lessons:
    01:13 1: Try it!
    03:28 2: Check your loop bounds
    03:46 3: Stress test your algorithms
    06:09 4: Big O is not the full story
    07:42 5: O(n) ≠ O(L)
    09:17 6: Preprocess your data
    12:09 7: Big O is approximate
    13:53 8: Big O is for big inputs
    17:44 9: SIMD is awesome

    • @C0ckfuck
      @C0ckfuck Год назад

      I think that you should mention that big O notation really is really nothing more than a set of functions of a "certain shape", and that the shape of your expression measuring what you want (running time, space complexity, number of steps etc.) is in that set if it is asymptotically dominated by functions in that set.
      More importantly, I think it is quite harmful to bring up the notion of the average case runtime for a deterministic algorithm over a distribution of inputs as if it is says anything meaningful - since how do you define the input distribution, and what does it tell you? Nothing at all for a deterministic algorithm.
      You can flip the picture and consider an algorithm like the one you describe but where its input is considered a cyclical buffer (just start over when you reach the end...). Then you could have it pick an index uniformly at random and do average case analysis based on the random choice. This is a good example of both probabilistic analysis of running time and of the advantages of a randomized algorithm.

    • @strager_
      @strager_  Год назад +2

      > I think that you should mention that big O notation really is really nothing more than
      Plenty of other videos explain it this way. I wanted to explain it a different way which I think is more intuitive.
      > I think it is quite harmful to bring up the notion of the average case runtime for a deterministic algorithm
      I brought up average time complexity after talking about best and worst case. I wanted to show that, given a best case of O(1) and worst case of O(n), average case is *not* necessarily 'in the middle' of best and worst. Average case time complexity was not O(n/2) or O(sqrt(n)) or something; it was just O(n).

    • @C0ckfuck
      @C0ckfuck Год назад

      @@strager_ But wait, O(n/2)=O(n). I think intuition is fine, but I think it's important to mention that that it is not the whole picture.
      Regarding your use of average case analysis, my point is that, sure, you can perform a thought experiment - but I think it is important to mention that it only has any merits when comparing algorithms w.r.t. a "meaningful" distribution and that it is very hard to define. My concern would be that viewers would be inclined to use uniform distributions over input spaces when they see it done this way when this is not mentioned. Anyways, take it for what it's worth. People seem to like your video, and so do I.

    • @kameronbriggs235
      @kameronbriggs235 Год назад +2

      I'm primarily specialized in graphics programming & self taught. Got major ADD issues, and can't even remember the months cuz what's the point?
      Your formatting is perfect. It keeps me entertained, and you're good at speaking, all while teaching and representing ideas in simple ways (: sick af dawg, dis gnarly.

    • @strager_
      @strager_  Год назад +2

      > I think it is important to mention that it only has any merits when comparing algorithms w.r.t. a "meaningful" distribution and that it is very hard to define.
      Agreed. I didn't make that nuance clear in my video.

  • @moe_31415
    @moe_31415 Год назад +179

    Big O is not bad nor a lie, you just have to understand it. It does not tell you how fast an algorithm is. It only tells you how it scales with respect to its input size

    • @ApesAmongUs
      @ApesAmongUs Год назад +18

      Yea, it took me a moment to understand what he was saying in the screen shot, since it never dawned on me that anyone had ever believed the simplified (and therefore incorrect) "myth" as he was using it.

    • @Garentei
      @Garentei Год назад +29

      The whole video is a strawman take on Big O. Debunking and correcting claims nobody actually believes, and if they do, they are misunderstanding it.

    • @ClaudioParraGonzalez
      @ClaudioParraGonzalez 11 месяцев назад +6

      this video proves that youtube is full of people that teaches from ignorance.

    • @Meneer456
      @Meneer456 9 месяцев назад +4

      C'mon guys, everyone uses some way to lure people to watch their video.
      All in all, great video! A lot can be learned from this video, I think you'd be a great teacher if you aren't already.

    • @jordixboy
      @jordixboy 9 месяцев назад +2

      @@Meneer456 yeah, why people so toxic and calling him strawman huh? bad day? Why dont you get in front of a camera, lets see how that goes.

  • @jacklegate3324
    @jacklegate3324 Год назад +205

    The biggest thing to take away is that big O is not equivalent to the actual performance, it is an upper bound for the growth rate of an algorithm. When comparing algorithms look at the behavior when you double the amount of input.

    • @louisrobitaille5810
      @louisrobitaille5810 Год назад +3

      Usually it becomes way more obvious if you decuple it instead of just doubling it 😅.

    • @KingJangOng
      @KingJangOng Год назад +1

      yea everyone seems to forget this

    • @tear728
      @tear728 Год назад +16

      Isn't that the entire point of big O lol how could anyone forget this

    • @KingJangOng
      @KingJangOng Год назад +10

      @@tear728 they never actually learn what it is

    • @EthanBradley1231
      @EthanBradley1231 Год назад +8

      @@tear728 I can't remember who it was but I recently saw a video where someone said they almost failed their Google interview because *the interviewer* didn't understand this. The question was something like "Algorithm A runs in O(n), algorithm B runs in O(logn). What's faster?" They correctly answered that B is better asymptotically but A might be faster for small inputs, and the interviewer insisted that was wrong.

  • @skrundz
    @skrundz Год назад +205

    This is like how they discovered a new “efficient” multiplication algorithm for large numbers but the constants are so huge it’s only faster once you’re multiplying numbers over 4.5 trillion bits long

    • @strager_
      @strager_  Год назад +47

      Yup! Luckily, I haven't needed to deal with data sizes that big. 😅

    • @RepChris
      @RepChris Год назад +57

      Yup. Those "galactic algorithms", as theyre called, dont really have any direct real world use; they however are useful for the theortical part of computer science such as by showing that certain bounds can be broken, or by giving a framework of techniques by which practical algorithms can be newly developed or improved further. Theres also the chance that at one point well have to deal with such huge inputs, but as with abstract mathematics the usual way they become useful is by laying the groundworks.

    • @kuhluhOG
      @kuhluhOG Год назад +9

      @@RepChris probably if you want to have highly accurate (as accurate as Quantum Physics let's you be) Physics simulations, these can be useful

    • @RepChris
      @RepChris Год назад +8

      @@kuhluhOG Yeah, but that falls under the "we might get to that amount of input data at some point" category, as we currently very much do not use even remotely the amount of input in any application to make a galactic algorithm faster; that is somewhat tautological since thats a part of the definition of what a galactic algorithm even is.

    • @kuhluhOG
      @kuhluhOG Год назад +4

      @@RepChris it was more of a comment on your sentence "Theres also the chance that at one point well have to deal with such huge inputs"

  • @sepulous
    @sepulous Год назад +34

    It's important to keep in mind that Big O is for classifying the scalability of algorithms, not their performance in a given context. Big O analysis is not always a substitute for a benchmark.

  • @KyttaIsHere
    @KyttaIsHere Год назад +192

    When I got taught big O at the university, many people (including me) failed to comprehend the actual meaning of it (some still don't). One could replace, like, 2 lectures worth of material with this one video. Keep up the good work!

    • @strager_
      @strager_  Год назад +11

      Thanks!

    • @marloelefant7500
      @marloelefant7500 Год назад +33

      I personally always found O notation one of the easiest things in theoretical computer science.

    • @bluepancake975
      @bluepancake975 Год назад +6

      at our uni we weren't even taught what big O actually means (what is explained in this video), instead we were taught to blindly follow all the myths...

    • @nexdemise4182
      @nexdemise4182 Год назад +4

      The easiest way to understand big O in my opinion is to not just focus on the most significant term, but rather write out the entire time complexity, so instead of O(n^2) it'll be something like 4n^2+80n+20n*ln(n)+35. That kinda helps understand that the constants do matter and can overpower the n and which term is the most significant for a given n. Like O(n) isn't better than O(n^2) if O(n) is 8,000,000n and O(n^2) is n^2 unless n > 8,000,000.
      Like take this scenario, you have some counting function that relies on sorting and it's O(n*ln(n)), is it worth the effort in switching to a priority queue to get that O(n)? That depends on what you expect n to be. If this stuff will involve at most a thousand items, probably not unless there's some serious time critically going on (which generally there won't be), if it's going to involve billions then yeah.

    • @joeymoore7974
      @joeymoore7974 Год назад +4

      Big O notation says that, given a function f and a function g, where f(n) is in O(g(n)), there exists some constants c and k such that f(n)≤c*g(n) for all n>k. I never found it hard to understand or formalize, but I can see why it seems hard to understand. In general, big O notation only matters when you have differing time complexities and large values of n. It tells you that, as n grows larger, a function that is not in O(g(n)) is going to necessarily be larger than a function that is in O(g(n)).

  • @atiedebee1020
    @atiedebee1020 Год назад +319

    This is one of the greatest programming channels, keep up the good work!

    • @superspartanman4480
      @superspartanman4480 Год назад +8

      The greatest

    • @wernersmidt3298
      @wernersmidt3298 Год назад +21

      Every person I've seen using comic sans in a presentation is an absolute beast. Like you have to earn your right to use it.

    • @strager_
      @strager_  Год назад +19

      @wernersmidt3298 It's an honor to be compared to Simon Peyton Jones!

    • @fredoverflow
      @fredoverflow Год назад +3

      @@strager_ So... are you gonna make a Monad tutorial next? 🙃

  • @qubyt
    @qubyt Год назад +60

    One of, if not the best big O notation video out there. Bravo and thank you!

  • @mikhaililin3033
    @mikhaililin3033 Год назад +11

    This channel is pure gold. Clear explanations, starting from simple problems and going to more sophisticated ones, step by step, while not focusing on little details for too long and constantly tossing in some new questions for viewer to think about. Thanks for your effort

  • @kasuha
    @kasuha Год назад +38

    O notation might be for big data but different O complexities have different Ns that count as big. N=50000 may not matter for difference between O(N log N) and O(N log log N) but it is big enough to make a difference for O(N^2).
    I still have PTSD from times when I spent hours upon hours waiting for my Windows to update because someone in Microsoft deep down the history decided that number of updates will never be big and used some kind of bubblesort to sort them.

    • @strager_
      @strager_  Год назад +8

      > different Ns that count as big
      Agreed! But the notation doesn't give you *any* clue where this point might be.

    • @user-zh5kg2op4h
      @user-zh5kg2op4h Год назад +2

      Actually, there are still some places in Windows like this, that someone have 10 minutes opening Start menu in some conditions. Or Explorer starting to slow down when doing something like deletion in a folder with thousands of files. (Explorer meaning desktop as well.)

    • @gianni50725
      @gianni50725 5 месяцев назад

      You know, I think it's dumb they used bubble sort (at least use insertion sort!), but I really have to wonder how many updates you missed if bubble sort was enough to slow down the time to update significantly.

  • @andrasfogarasi5014
    @andrasfogarasi5014 Год назад +5

    Wanna hear the best thing? It's Levin's universal search. It can solve any computable problem in an optimal time complexity. The only downside? The universal search adds a constant element which scales exponentially with the length of the optimal program. If the optimal program is L bits long and achieves O(f(n)) time complexity, Levin's universal search will achieve O(f(n)) time complexity, but with an added 2^L cycle delay. This delay quickly gets absurd, so the universal search has no practical application.

    • @strager_
      @strager_  Год назад +2

      Gotta love ivory tower computer scientists!

  • @randolphbusch7777
    @randolphbusch7777 Год назад +5

    The reason you don't need the log base is because you can convert any log to any other log using a constant factor - which is ignored in the O-notation.
    log_e(t) = log_2(t) / log_2(e)
    log_2(e) is a constant. In O-notation, log_2(t) and log_2(t)/C are the same, which means log_2(t) and log_e(t) are the same.

  • @andytroo
    @andytroo Год назад +16

    my favorite "non linear" algorithm is manipulations within the Disjoint-set data structure - it has an average and worst case of O(a(n)) where a is the inverse Ackermann function -- functionally less than 5 for any number you can write.

    • @minerscale
      @minerscale Год назад +4

      Wow that's amazing hahaha. At that point it may as well be constant time. But no, an infinite input size will have an infinite lookup time. Incredible.

    • @riverm5889
      @riverm5889 Год назад

      I think they use the inverse ackermann function for amortized analysis which basically just treats it as a constant :)

  •  Год назад +29

    Great explanation!
    One aspect that isn't talked about much is that due to laws of physics the actual *random* access time (in seconds) to a pre-computed table of size n is O(sqrt(n)), not O(1). As a result there are algorithms that are actually slower in real time for large inputs even though they are predicted to be faster by big-O.
    There's a great article called "The myth of RAM" that explains it deeply.

    • @strager_
      @strager_  Год назад +4

      In modern computers, are cache effects what you mean by sqrt(n)?
      The simplicity of RAM (Random Access Machine) which we normally use for complexity analysis is indeed deceiving. My case study didn't lend itself to an explanation of RAM (or CPU caches), so I kept it simple and focused on other issues with time complexity.

    •  Год назад +5

      @@strager_ yes, it's cache but the article I mentioned explains it can't be fixed by making cache bigger because of quantum physics (maximum amount of information in black hole is proportional to its surface area and nothing can be denser).
      And yes, I understand that this is introductory video so advanced stuff like this wouldn't fit in there.

    • @elkeospert9188
      @elkeospert9188 Год назад

      If I understood that "great article" correct (I only had a quick view on it) the reason for that O(sqrt(n)) is that memory (RAM) is typically flat - so the double amount of memory needs the double area. If these area is a square when doubling the memory causes that the width of this square is increasing by factor SQRT(2) - and therefore the average physical distance the information has to "travel" is proportional to SQRT(2) of the size of the memory.
      As information could not travel faster than the speed of light that really would mean that the access to memory is getting slower the larger the memory is....
      On the other side transistor density on chips is still increasing - double memory therefore not mean that the double area is needed - that might will no longer true in the future but for the next years O(1) makes more sense than O(SQRT(n)) for memory access.

    •  Год назад

      @@elkeospert9188 almost, circle is a better representation but when we use big-O we don't write constants, so we drop Pi.
      The article has other parts, where the author explains that quantum physics dictates it will never be O(1) and stay O(sqrt(n)). Increased density of transistors helps only by a constant. If you need to process 100PB of data, you can't fit it into RAM and will need to use other storage which will be slower.

    • @jsgfitz
      @jsgfitz Год назад

      @@strager_ Even ignoring physics fundamentals, if you're using virtual memory the pagetable lookups are O(ln n) so the O(1) claim for lookup tables was always suspect. Of course caches - incl TLB - mitigate this in practice, so long as you fit within it.

  • @fireballs619
    @fireballs619 Год назад +28

    I'm a physics PhD student who has to code all the time despite having taking very few formal courses on it. I find your videos super helpful and have been learning a lot!

    • @43chord
      @43chord Год назад +1

      Let me guess, fellow HEP enjoyer?

    • @fireballs619
      @fireballs619 Год назад +2

      @@43chord cosmology :)

    • @mastershooter64
      @mastershooter64 Год назад

      ​@@fireballs619 what do you do in cosmology?

    • @fireballs619
      @fireballs619 Год назад

      @@mastershooter64 CMB analysis mostly

  • @Cubinator73
    @Cubinator73 Год назад +3

    7:51 "[...] and, in fact, you have fewer lines than you have bytes"
    A file containing only
    always has one more line than bytes. In fact, an empty file has no bytes at all, but one (empty) line.

  • @bibliophile5863
    @bibliophile5863 Год назад +9

    Obligatory comment noting that in the manually unrolled loop, the code may reach outside of the array bounds, resulting in a crash, if you're lucky. This is still one of the best complexity explanations I've seen.

    • @strager_
      @strager_  Год назад +12

      > the code may reach outside of the array bounds, resulting in a crash
      Yes, that is true. I didn't explain this in the video, but when generating the table, I added 7 dummy elements to prevent out-of-bounds issues. github.com/strager/big-oh-lines/blob/537e6a048d10c89ee6ea421c48c4ecc7229d2d01/bol_linelinear8/src/lib.rs#L24-L26

    • @k6l2t
      @k6l2t Год назад

      @@strager_ alternatively you can just modulus the SIMD size, and do the non-SIMD naive stuff on the remainder, if you don't want to do array resizing nonsense

  • @vishwanathbondugula4593
    @vishwanathbondugula4593 Год назад +19

    Hey while we are traversing through the string, as soon as we hit a
    char we can store a counter which increments for every line and if we want to show the line number just use the counter variable which will not take any time

    • @NamelessStudiosInc
      @NamelessStudiosInc Год назад +5

      definetly possible, but usually in compilers there is a single pre processing step before the representation of the code changes. Compile time errors, as far as I know, are usually not found until the second step of compilation

    • @strager_
      @strager_  Год назад +34

      > we can store a counter which increments for every line
      That is true, but in the real world it's usually more complicated than that.
      Context: A compiler in practice does not always report errors immediately. It might need to parse some code then report an error in the past. (For example, Rust cannot report a use-of-undeclared-function error until it reads the entire file because the function might be declared after the call.) Therefore the compiler needs to attach location information to each token and AST node.
      In addition to line information, a compiler also wants to report column information. The compiler *could* attach two numbers (line and column) to each token and AST node. But some IDEs want byte offsets, so we might need to store three numbers (line, column, and offset from beginning). That's a non-trivial amount of data to add to *every* token and AST node, so memory usage goes up.
      To reduce memory usage, a compiler could attach only *one* number (offset) to each token and AST node, and compute the line number and column number as needed. (Or not compute anything if the IDE just needs a byte offset.) Hence the table-based solutions discussed in the video.
      But that's for a traditional batch compiler. Nowadays compilers are integrated into editors. Compilers are given *updates*. LSP (Language Server Protocol) updates communicate line and column information *into* the compiler. Having the line table data structure makes this operation much cheaper.
      I didn't discuss any of this in the video because I didn't want to bore people who aren't interested in compiler frontends. But it's a good discussion. =]

  • @ssmith5048
    @ssmith5048 Год назад +4

    Fantastic presentation and explanation! Although I watched you put this together, seeing the final version was even more engaging than I expected. Great work and thank you!

  • @peter8261
    @peter8261 Год назад +1

    Dude showing the y=mx+b and the graph and then showing in as o(n) shattered my brain. That makes so much sense.

  • @viraatchandra8498
    @viraatchandra8498 Год назад +5

    Thanks for this Amazing Video!! I am a fan of your channel, and love the kind of content you have been putting out recently Its really good to see you uploading more often, cant wait to see what you share with us next!

  • @Yotanido
    @Yotanido Год назад +77

    Didn't learn anything new about Big O, though the reminder is nice. What I did learn, though, was how easy it actually is to use SIMD, at least for simple applications like this.
    I've tried looking into it in the past and just got super confused.
    I have to say, a video specifically on SIMD would be awesome.

    • @strager_
      @strager_  Год назад +44

      > a video specifically on SIMD would be awesome.
      My previous video on hash tables also contained some SIMD stuff. That hash table SIMD was more advanced, but I didn't walk through the code. ruclips.net/video/DMQ_HcNSOAI/видео.html
      I do want to share more SIMD knowledge in future videos. 👍

    • @Dorumin
      @Dorumin Год назад +6

      The portable SIMD struct that nightly rust has is awesome.

    • @treelibrarian7618
      @treelibrarian7618 Год назад +1

      @@Dorumin yeah, intel should've named the vbroadcast intructions "splat" instead lol

    • @Dorumin
      @Dorumin Год назад

      @@treelibrarian7618 I was talking more about the structured approach to lanes and its ops over intrinsic instructions on small buffers, plus the nice Into/Froms. The names are funny though, what do you think about the swizzle trait? :P

    • @andremaldonado7410
      @andremaldonado7410 Год назад

      ​@@strager_ I would love to see it, something I'm very interested in learning more about

  • @enclave2k1
    @enclave2k1 Год назад +6

    Certified strager classic.

  • @BeccaYetiammo
    @BeccaYetiammo Год назад +3

    Wow, I’m actually planning on optimizing a batch processing API for tomorrow and I came across this video. Learned new insights that will help me make decisions on optimization. Thank you for this! 🙌

  • @ivanraulsanchezdiaz7499
    @ivanraulsanchezdiaz7499 Год назад +4

    I met you on twitch and you even helped me with a terrible code that I wrote, I already knew you were like a genius but what you are doing on this youtube channel surpasses everything!

  • @kennethbeal
    @kennethbeal Год назад +7

    Loved your ending! Take that, Rust Foundation! :) Thank you for the Big O lesson as well.

  • @fridgedigga
    @fridgedigga Год назад +5

    Really loving the past few videos. Learned a ton. Thanks.

  • @trudyandgeorge
    @trudyandgeorge Год назад +3

    Mate, you've got the nack for educating. I'm loving the channel.

  • @davidkormendi354
    @davidkormendi354 Год назад

    Really love your optimization videos and the way you explain. Keep it up my man!

  • @user-vx1vl1ci1e
    @user-vx1vl1ci1e Год назад +2

    This was an absolute treat to find in my notifications after work. Thank you

  • @kanuos
    @kanuos Год назад

    I am so glad I found you from your hash table video. Concise, informative content, with proper suggestions and pop quizzes to keep the viewer entertained. What more could you ever want?!

    • @rovalen_hagane
      @rovalen_hagane 9 месяцев назад

      An extra thicc rust crab sticker, probably

  • @ryanjupton
    @ryanjupton Год назад +2

    Man, that's one of the most succinct, best explanations I've heard. Keep posting!!!

  • @protosssc790
    @protosssc790 Год назад +2

    Thank you for the helpful information learning these subject alone sometimes feel depress but watching contents like this gives me hope thank you again!!

  • @apolloapostolos5127
    @apolloapostolos5127 Год назад +1

    Having started ThePrimeagen’s course on Algorithms & Data Structures, this was a a pleasant addition for me.
    .
    I like that you did a pop quiz ‘before’ the material. This follows the research of learning effectively as described by Make it Stick and other books.
    .
    👏👏👏
    This week I learned ways to optimize if-statements in Bash Scripts for a CPU prefetcher-unit’s performance, and about misprediction penalties for the branch-priority features.
    .
    And I’ve only started learning to script/code/program for a couple months.
    .
    I’m so grateful for the efforts being out into maintaining and contributing via the internet so I could access all this! Whoo Hoo! 🤠

  • @kilaposhi
    @kilaposhi Год назад +2

    That is so good, super clear and super interesting ! Love your work

  • @xoca7171
    @xoca7171 Год назад

    These are some good informational videos. I really like the fast paced nature of them, focused on the question at hand. I feel I can learn in 20 mins what would take me a few hours of watching some other videos.

  • @mash808
    @mash808 Год назад +3

    This is probably the most helpful time complexity video I've watched to date (and I've watched a lot). Thanks so much.

  • @thefrigginguy
    @thefrigginguy Год назад +2

    I love the build up of information and background of a problem.

  • @angryanonymous4082
    @angryanonymous4082 Год назад +2

    This was randomly recommended to me, and it was one of the best explanations on big O I've seen

  • @aronas1353
    @aronas1353 Год назад +1

    I love your videos, your explanation is straight forward and comprehensive.

  • @user-zw8uq1rj9m
    @user-zw8uq1rj9m Год назад +2

    Everytime I watch your vid, everytime I get a tons of insights from you.
    Thanks and please keep going!

  • @zdspider6778
    @zdspider6778 Год назад +1

    Interesting stuff. Thank you for demystifying some of these concepts.

  • @polyfoxgames9006
    @polyfoxgames9006 Год назад +7

    For the array lookup, wouldn't you store the line number at each position not the offset?

    • @strager_
      @strager_  Год назад +4

      You're right! I messed up the explanation. 🤦‍♀️

  • @alienrenders
    @alienrenders Год назад +38

    Big O is meant for describing limiting behaviour as it reaches infinity. We never use infinite elements. So there are plenty of algorithms that could have worse Big O notation and be faster because we use fewer elements. I personally never liked Big O notation in Computer Science because it often detracts from practical algorithms. Nice presentation.
    On computers, it's really difficult to know what will speed something up sometimes :)

    • @oscarfriberg7661
      @oscarfriberg7661 Год назад +5

      Big O is also meant to describe worst case scenarios. Maybe the worst case is vanishingly rare for your particular use case, so optimizing for Big O might not lead to any speed improvements. Context matters!
      At best, Big O can be used as a guidance, but should never replace real analysis and measurements on actual real world data. You lose so much information about an algorithm by trying to describe it with just a few symbols.

    • @shiinondogewalker2809
      @shiinondogewalker2809 Год назад

      @@oscarfriberg7661 even in that case, it's specifically for the case where the input/problem size is sufficiently large

    • @gillbates2685
      @gillbates2685 Год назад +3

      @@shiinondogewalker2809 people who complain about big O's practical value often have no understanding what it means. Big O doesn't need anything in your program to go to infinity. The information it gives you becomes true for input size above N, what N is, depends on your formula. Could be 10, could be 10^10.
      People who think "it only matters for infinite value" remind me of debacle at Microsoft, some "practical programmers" who thought the number of updates will never be "sufficiently large" so they used the wrong sort algorithm leading to millions of people waiting hours, days for windows updates 😂

    • @gillbates2685
      @gillbates2685 Год назад +2

      ​@@oscarfriberg7661 big O tells you the upper bound, while big Omega telling you the lower bound. So any argument against big O citing worst case scenario could be reverted into argument for big Omega, now you optimize for lower bound, for large input your algorithm will always be at least as good as the value you optimize for :))
      And to many people big O and big Omega and big theta are all "Big O notations", so when they spout "Big O are useless" they mean any asymptotic notations.

    • @gillbates2685
      @gillbates2685 Год назад +2

      "Big O notation in Computer Science because it often detracts from practical algorithms"
      Big O detracts from practical algorithms the same way the ability to count detracts from practical algorithm
      There are two stages when you design an algorithm
      the theory stage, where you use reasonings in discrete mathematics to derive the right computation then formally prove the time/space complexity (if you skip this stage it's because some smart person already did it for you and wrote it in a textbooks/papers somewhere then other people repeat it into wisdom and eventually it reaches you without you having to open a textbook).
      And the implementation stage, where you take into account what the compiler does, what the CPU does.
      Big O doesn't care about algorithm or its implementation, it cares about functions f(n), g(n) on (typically) natural numbers. It is a mathematical concept, much like counting, both deal with natural numbers, and both are used to estimate.
      Now guess which stage are big O and counting more often used in :))
      Probably stage 1, but it does help in stage 2 too, it gives you a direction on which practical things to try when you don't have all the technical details of your machine, what kind of optimization is done on your CPU etc.
      because in practice, you don't start out knowing all these technical details. If you already knew like in this video, what kind of sampling distribution your input data have, how fast your algorithm runs on this specific CPU, then why would you go back to look at the more general idea which makes no such assumption about your system? if the aim is to disprove the theory using practical implementation I suggest at least understand the theory first. e.g. big O never implies O(log N) is faster than O(N^2), that's stupid, an algorithm in O(1) is also in O(N^2)...
      Or of course you could spend time running your algorithm on billion different input sizes, that's very practical too :))
      Now do I need someone who is good at discrete math and algorithm on my team? no I don't. Do I want someone who doesn't understand the basics such as what big O notations tell you... absolutely not, that's a recipe for disaster.

  • @aleksandermirowsky7988
    @aleksandermirowsky7988 Год назад +1

    Excellent video! I found it to be perfectly balanced in terms of explanation, providing just the right amount of clarity without becoming overly lengthy or tedious.

  • @NaourassDerouichi
    @NaourassDerouichi Год назад +2

    I've followed the making of on Twitch. Glad this is out!

  • @_fudgepop01
    @_fudgepop01 Год назад +1

    I love the presentation, and I also love the dratini shirt

  • @brecoldyls
    @brecoldyls Год назад

    Really good video! I found this explanation of big o a little unconventional but good. I especially enjoyed your visuals and your dive into SIMD at the end. Thanks!

  • @silvis5749
    @silvis5749 Год назад +6

    Best big O notation i ever watched, thank you very much. ❤

  • @cw2448
    @cw2448 Год назад +2

    This video is an excellent explanation of Big O notation and what it really means for our algorithms. Great stuff here!

  • @letsaram
    @letsaram Год назад

    Your tone and editing make this comfy.

  • @benjaminpedersen9548
    @benjaminpedersen9548 Год назад +1

    Excellent video!
    About leaving out the logarith base, you do it because changing the base only changes the function by a constant factor.

  • @SteinGauslaaStrindhaug
    @SteinGauslaaStrindhaug Год назад +7

    In my experience Big O is very rarely a useful metric in web frontend code, since frontend deals mostly with interactivity the number of "things" is relatively small and low latency and minimising network traffic is almost always more important than number crunching efficiency. In fact for almost all DOM related things; if the number of objects is even barely approaching a size large enough for Big O to be meaningful; the DOM tree is probably so large it will kill most mobile browsers for excessive memory use. Also since your code runs on any random client you don't control how fast it's hardware is so most number crunching is better done on the server (in which case it's the backend developers problem 😉)

    • @strager_
      @strager_  Год назад +3

      This is a great point!

  • @jasonavina8274
    @jasonavina8274 Год назад

    This is the best introductions to Big O I've ever seen or read. Thankyou.

  • @luisr1421
    @luisr1421 Год назад

    This video was awesome! Thank you so much for the great info, I learned a lot.

  • @thromboid
    @thromboid Год назад

    Wow, this is as clear, concise, and comprehensive as I could imagine!

  • @DavidSpry
    @DavidSpry Год назад

    The past two videos have been great!

  • @CaleMcCollough
    @CaleMcCollough Год назад

    This is EXCELLENT! Thank for the information.

  • @adamodimattia
    @adamodimattia Год назад

    Totally agree, the best channel I bumped into in years!

  • @alejandrom.4680
    @alejandrom.4680 Год назад +1

    As a CS student, I truly appreciate this way of explaining theorethical computer science. Thank you so much!

  • @maksimsivyy5684
    @maksimsivyy5684 Год назад +1

    I was expecting to see SIMD binary search :D
    But the video is a great refresher, thanks for it!

  • @dukemo6551
    @dukemo6551 Год назад

    Love your videos. I learn so much!

  • @NoteFox_
    @NoteFox_ Год назад

    thanks for making this video, helped me remember a lot of useful stuff from university ^^

  • @VioletCatastrophe
    @VioletCatastrophe Год назад +1

    As someone with a background in mathematics, this was all... kind of trivial to me. But I see misconceptions about big O notation all the time from programmers so I can imagine this being very much needed. It's really just about limiting behavior, the same tricks mathematicians use to tame infinities, divisions by zero, and all that. It feels absolutely absurd to say it... but the actual specifics of implementation, and real world performance metrics are FAR more important than a cool mathematical trick used for shenangians. Anyway, very good video, and a very needed one to try to push against the absurd hyperfocus on big O time complexity over more relevant discussions about overheads and actual implementation.

    • @peteschaefer
      @peteschaefer 2 часа назад

      That misconception comes from the theoretical CS community. They talk only O(.), all the time.
      For good reasons, but it's also -in no small part- arrogance.
      Similar thing with pure vs. applied mathematicians.

  • @lc9245
    @lc9245 Год назад +1

    I remember there was someone who also made a video about how they were frowned upon by an interviewer for mentioning that Big O is just about how algorithms scales and not comparing how they perform in all cases. It shocked me because it seems almost common knowledge.

  • @RenannPrado
    @RenannPrado Год назад

    Wow. Amazing video! Good stuff!

  • @parthsharma7366
    @parthsharma7366 Год назад

    U earned a subscriber!!🤟🔥🔥
    Keep growing!!😊

  • @PragandSens
    @PragandSens Год назад +8

    Amazing dude.... how do you make those graphics? In case you use Adobe Premiere or any other editing software for it... what software do you recommend for drawing graphics like it?

    • @strager_
      @strager_  Год назад +5

      I used Microsoft PowerPoint and Motion Canvas.
      I love PowerPoint. I'm new to on Motion Canvas.

  • @TheTyrori
    @TheTyrori Год назад +1

    Extremely informative and intuitive presentation. Thank you! :)
    However, I'll admit I'm not as familiar with SIMD stuff, so the speed at which that was moved through left me with some questions.
    Will probably look into that on my own, but I would love to see something on that if it is a topic you'd want to cover in the future too!

    • @strager_
      @strager_  Год назад

      > the speed at which that was moved through left me with some questions.
      Thanks for the feedback!

  • @hamzadlm6625
    @hamzadlm6625 Год назад +1

    I love you man, keep on uploading please

  • @greob
    @greob Год назад

    Super interesting, thanks for sharing!

  • @hazzxd
    @hazzxd Год назад

    Just found your channel and I'm hooked.

  • @AlphishCreature
    @AlphishCreature Год назад +1

    I really like how this video explains how better big O doesn't necessarily mean better performance for data one's working with, while at the same time it doesn't overdramatise things. As video states, big O describes the general shape of performance curve with increased data, but it isn't be all and end all. And as SIMD example shows, even if it has the exact same big O parameters as line table, it makes up for it with what I call "small c", i.e. those "insignificant" constant factors like the slope.
    Personally, my intuition is to prefer structures and algorithms with better big O parameters for key operations - the idea being that for most scenarios either the data is so small extra constant costs are negligible, or the data is so big that better big O matters. But like most things, it's not an absolute rule and in the end, it takes real life performance testing to get the most complete picture.

  • @user-el2vh7sj8e
    @user-el2vh7sj8e Год назад +2

    Really good video. Thank you!

  • @Klosterhasi
    @Klosterhasi Год назад

    this was a really smart way of making me watch a video on big O notation nice job

  • @haystackdmilith
    @haystackdmilith Год назад

    I must admit I found your explanation very straightforward and easy to understand.

  • @carterpatterson1988
    @carterpatterson1988 Год назад

    You got a new subscriber, fantastically informative video ty sir

  • @tuncaybasak4953
    @tuncaybasak4953 10 месяцев назад

    Hope for more video coming on RUclips, really appreciate them 😊

  • @padraighill4558
    @padraighill4558 Год назад +2

    the base of the logarithm doesn't matter because it is just a multiple and we are already omitting coefficient!
    also as a mathematician I would have loved to see Landau's Big-O notation in its actual form.
    we say that f(x) = O( g(x) ) as x -> ∞ if
    there exists an M > 0 and an N > 0 such that
    if n > N then f(x)

    • @strager_
      @strager_  Год назад +1

      Yeah, I didn't explain clearly why we don't care about the log base.

  • @mantas9827
    @mantas9827 Год назад

    Great explanations !

  • @greyrabbit2157
    @greyrabbit2157 Год назад +2

    I love how you use the shrimp emoji for the left arm muscle flex. Great video!

    • @strager_
      @strager_  Год назад

      You are the first one to point this out! 💪😍🍤

  • @AndreLaBranche
    @AndreLaBranche Год назад

    Very lucid, admirable density :)

  • @hamzaaitboutou8563
    @hamzaaitboutou8563 Год назад

    information quality over 9000! pure signal, no noise

  • @walterfairfax6043
    @walterfairfax6043 Год назад

    Love this type of videos, please more :)

  • @johanrojassoderman5590
    @johanrojassoderman5590 11 месяцев назад

    Creel has made a pretty nice video touching on the subject as well, regarding sorting algorithms.

  • @MaxPicAxe
    @MaxPicAxe Год назад

    This was a much better video than i expected

  • @DMSBrian24
    @DMSBrian24 Год назад

    Really awesome video!

  • @mayank8387
    @mayank8387 Год назад +2

    In java, the program still works when there are no new lines, meaning we could essentially write the entire program in one single line. I wonder how the compiler detects error in such case.

    • @cjreek
      @cjreek Год назад +9

      The compiler doesn't care. It'll just report all the errors being in line 1. It's the programmers problem to deal with that.

    • @leddoo
      @leddoo Год назад +2

      you usually get line & column information. once you know the line, you can just compute the column using some simple arithmetic (error_offset - line_begin + 1).

  • @ttrss
    @ttrss Год назад +4

    great channel, these are really interesting . Also why is it so rare for the compiler to optimise with SIMD instructions, are those comparison blocks just not a common code pattern?

    • @strager_
      @strager_  Год назад +1

      > Also why is it so rare for the compiler to optimise with SIMD instructions
      In this particular case, there are a few reasons why autovectorization is a problem:
      * The compiler cannot reasonably assume that the Vec's length is a multiple of 8. Supporting any Vec is complicated.
      * Because of the early return, and without profiling data, the compiler might think that it's common for the *first* element to return, therefore SIMD isn't worth it.
      * The compiler is written by humans with limited time, so the autovectorizer is implemented for common code patterns. I suspect my algorithm isn't common. (Maybe it's common with '==', but not with '

  • @ahmadumar9387
    @ahmadumar9387 Год назад +1

    great explanation :D

  • @globnomulous
    @globnomulous 6 месяцев назад

    Title is a bit clickbaity but I really appreciate your channel. Not a lot of people go to the lengths you go to to help viewers(/readers) really see the thought process and the knowledge that contributes to it. These videos, like the best documentation and code comments, makes both 100% clear.

  • @axu6207
    @axu6207 Год назад +2

    Thank you for making my day!

  • @vyldim3401
    @vyldim3401 Год назад +2

    I know this was just educational, I know this was supposed to showcase Big O and different approaches to programming. BUT if I dont say this, I'll lose sleep over it so here goes. You can just increment a variable by 1 everytime you encounter a newline while compiling. So when it encounters an error, you have the line number ready. Time complexity O( time to reach error ), Space Complexity O(1). You are designing a compiler here, I'm sure you can add a line or two to the code can't you?

    • @strager_
      @strager_  Год назад

      You would need to store that line number somewhere. Storing line+offset or line+column uses more memory than just storing the byte offset. That memory consumption is multiplied by every AST node, so it's actually O(n) space usage.
      Also, the line tables I describe in the video are handy for implementing LSP.

  • @cod1r
    @cod1r Год назад +1

    these videos are great!

  • @jonas-ke4qz
    @jonas-ke4qz Год назад +1

    fantastic video. subscribed

  • @stathere
    @stathere Год назад

    Thanks for the interesting content!
    I wish someone had explained it to me like this when I started my developer journey.

  • @Warwipf
    @Warwipf Год назад +2

    I didn't know there were misunderstandings about this topic. I guess my uni did a good job (thanks Prof. Schöning

  • @PH4RX
    @PH4RX Год назад +1

    7:14 even if you adjust the x-axis, you can't have a straight line because your x is discreet. You can't have 1.5 lines, so it would be be jumps as well.

    • @strager_
      @strager_  Год назад

      Yeah, I should have made it a scatter plot. Good point.

  • @abdulshabazz8597
    @abdulshabazz8597 Год назад

    All software (algorithms) perform transformations on data structures: 1. Choose the correct data structure to distill your data (eg vectors vs hash tables). 2. Choose the correct algorithms to operate on your data (eg. for-loop vs binary search). 3. (Advanced) Note which ones work best toward your design goals.

  • @jorgeosorio1613
    @jorgeosorio1613 11 месяцев назад

    Thanks a lot, men. Your videos are very useful.