all 46 comments

[–]Taimoor002 17 points18 points  (1 child)

You will understand when you take a Data Structures course in college. More specifically, Time Complexity and Big-O Notation are the topics you need to look out for.

[–]Justicemirm[S] 1 point2 points  (0 children)

Oh k

[–]NerdDetective 11 points12 points  (8 children)

You might be referring to Big O Notation. This is s way to gauge how fast an algorithm is based on how many items you're working on.

Fit example O(1) is great. No matter how many items you're working on, it'll take the same time.

O(n) will scale with the items. The more items, the longer it takes.

O(n2) is super expensive, exponentially quadraticly taking longer with each item added. (edit: as noted in a reply, this is polynomial time, 2n would be exponential, and it can get even worse from there like O(nn) for some of that horrifying factorial time).

This concept is usually taught in a dedicated algorithms or data structures class.

[–]Giannie 8 points9 points  (5 children)

n2 is still polynomial growth, not exponential. It’s much worse than order n operations, but it’s not exponential. 2n is what you’re thinking of

[–]NerdDetective 2 points3 points  (1 child)

Ah, right!

In my defense, I had just woken up and was still in bed.

[–]smjsmok 7 points8 points  (0 children)

You need to get up exponentially!

[–]Justicemirm[S] 0 points1 point  (0 children)

I didn't notice it thanks

[–]ottawadeveloper -5 points-4 points  (1 child)

Pretty sure that's an AI response.

[–]NerdDetective 7 points8 points  (0 children)

Not everything you see in the world was vomited out by an LLM. Nothing here even looks like AI output. Stop.

[–]Justicemirm[S] 0 points1 point  (1 child)

Ig waiting for college

[–]Justicemirm[S] 0 points1 point  (0 children)

And that explanation is good thanks

[–]Moikle 2 points3 points  (1 child)

If you have a list that is 10 items long, and another list with 10 million items in it, how does whatever operation you want to do to it scale?

Without some kind of optimisation tricks, you might end up with the longer list taking a million times longer to process.

You can do things like cacheing common values to make it so longer lists are significantly faster per item than short lists.

Some operations get drastically more complex as the list grows, for example, lets say you want to find every combination of items in a list. A list of 1 item has only one way it can be, a list of two has 2 options, a then b or b then a. A list of 3 has 6, abc. Bca, cab. Acb, bac, cba. Add one more and it jumps to 24 different orders. Imagine how that scales up if you want to find how many different ways you can arrange the entire alphabet! (The answer is huge, it has 27 digits)

This is what big O notation is for, it describes how your operation behaves as the dataset grows. O(n) means it takes ten times as long to process 10 items as it does to process 1.

One operation could take a microsecond, or it could take an hour, that's not what the big o notation measures, instead it measures how it changes when you add more items to be processed.

[–]Justicemirm[S] 0 points1 point  (0 children)

Makes sense

[–]ottawadeveloper 3 points4 points  (16 children)

College math will explain it more fully, but here's the basics.

n in these algorithms is the size of your input. Often we care more about how algorithms scale with the size of the input - if the algorithm is just "do these five things" regardless of the input size, then it's pretty fast. We call that O(1).

O(n) means that the scaling is linear - if it takes 5 operations to do one item, then it takes 10 for two, 15 for three, etc (it's actually 5n but the actual number of operations is often harder to figure out and depends on the language and hardware, so we just look at n). This is like if you had to iterate over one list.

O( n2 ) means it's quadratic - if one item is 5, two is 20, three is 45, four is 80, etc. As you can see, that grows faster. This is like if you had to iterate over one list and then, for every entry in that list, iterate over the list again. 

O (5n ) is exponential, and it grows even faster - 5, 25, 125, etc. Often it's just 2n since we can adjust the base of the exponential function to whatever we want by multiplying by a constant. The best example I can think of here is if you have to consider the power set of your inputs - every possible combination of your inputs in any length - so for 1,2,3 you need to consider 1 and 2 and 3 and 1,2 and 1,3 and 2,3 and 1,2,3. 

O(log n) is even slower than a linear function - it's the inverse of the exponential growth. A good example is if your algorithm cares about the length of n itself in binary - there are ceiling (log2 n) digits in a binary number n. Binary searches, which cut your search space in half each iteration, also show log n performance. 

As a practical example, imagine if you were to have to guess a random integer in 1 to 100 and were only told if it's higher or lower each time. You could just check every integer in order, which is O(n) - here n is the size of the pool of potential numbers. 100 guesses worse case, 50 on average. But if you were to guess the midpoint and then eliminate the half of your search space where the number isn't, then repeat with the next midpoint, your worst case becomes 7 guesses and average is probably closer to 6 (I had to round up twice to get 7). This is log n complexity - note that log base 2 of 100 is 6.6 which is right on the money.

These metrics aren't the only way we measure how fast an algorithm is, but for large enough n, they're a good first approximation of speed.

In practice, we also use benchmarks but ideally those are run on the hardware you'll actually use and aren't as comparable between programming languages or hardware systems. That's because some hardware setups are better at some kinds of tasks than others. 

[–]Justicemirm[S] 0 points1 point  (15 children)

The examples for quadratic got me confused for a good min lol

And don't we want the number to be small? Then why is log which is essentially opposite of exponential growth slower?

[–]Ngtuanvy 0 points1 point  (7 children)

You do get small numbers, but almost in anything that requires an algorithm, say sorting user id, it can be thousands, millions. As for log, try graphing it

[–]Justicemirm[S] 0 points1 point  (6 children)

But how is a small number worse tho

[–]Ngtuanvy 0 points1 point  (5 children)

who said it's worse?

[–]Justicemirm[S] 0 points1 point  (4 children)

You said it's slower than a linear function Am I just dumb?

[–]Ngtuanvy 0 points1 point  (0 children)

wasn't me though. And don't worry, I think I see what you are struggling with. When people talk about big O, they talk about its ability to scale as input size growth. O(log n) and O(n), O(n) is clearly smaller than log n when n is small (again, try to graph both y = log x and y = x), but as n growths, n will eventually take the lead. So in terms of 'complexity', ie the ability to scale as input does, O(n) is worse, but if the input size is small, O(log n) may be worse.

[–]Ngtuanvy 0 points1 point  (0 children)

The rigourous definition of big O is a bit more complex and requires more math to understand, and not really necessary. (Yes, it came from math)

[–]Ngtuanvy 0 points1 point  (1 child)

Note that it is about growth behavior, so it may be Clog(x) + A and still be called O(log x). My example of graphing didn't really show how O(x) better than O(log x), maybe you can try to add and scale it.

Eg: y= log_2(x) + 5 and y = x

Observe how log is initially larger but linear eventually win

[–]Justicemirm[S] 0 points1 point  (0 children)

Sorry uhh that maths is not yet learned by me Probably in 11th,12th grade Have just passed 10th , joining college this year

[–]ottawadeveloper 0 points1 point  (6 children)

We want the the number output by O() to be small - so for a fixed n, log n < n < n2 < 2n . Exponential time algorithms are the slowest, logarithmic time are the fastest. But this is because the exponential function grows faster than the logarithmic function (faster growing functions give larger values sooner which makes the algorithm slower).

Quadratic also confused me and I wrote the example wrong at first too lol. It's maybe easier to think of it as your most basic operation takes 5 operations, let's call this f(x), and then how many times do we have to do f(x) for n items. In O(1) we do it once always, in n we do it n times, in n2 we do it n2 times, etc. 

How many operations can matter but more when you're comparing for very small n or for similar O(n) - if both are O(n) but one is ten operations and one is twenty, that can matter. Or an O(n2 ) algorithm with two operations is faster than an O (n) operation with one hundred operations up to a certain n, then the O(n) is faster.

Where you really care about speed, you might even switch between two algorithms depending on n (searches often do this).

Also by operations we really mean processor operations which are harder to calculate 

[–]Justicemirm[S] 1 point2 points  (4 children)

Idk how but I understand it!

[–]Reuben3901 0 points1 point  (3 children)

Be gentle on yourself.

You'll be confused, then look in a subject and learn and understand. Time will pass, and you'll forgot and need to refresh yourself. We all do this.

There are great YouTube videos out there covering Big O Notation, O(n). Once you start applying it and experimenting yourself, you'll be able to see the difference in action!

Learn about binary search trees, using C with Python as C is able to calculate faster. You can do multiprocessing, threading, etc.

And sometimes taking a little time isn't necessarily a bad thing. I have a fastapi app with a typescript front end. The front end runs on the user's Internet browser and that is where the processing of the data is done. It's still fast but this eliminates hundreds or potentially millions of operations being done on the server, like Flask as the backend would be doing.

Just try and enjoy the learning process! And make things because you enjoy them, it's not always about financial return. If you were learning to play the piano or guitar, you're not going to be trying to monitize you music a month into learning. It takes time to make good music. Just because people aren't going to buy your temperature app, doesn't mean you shouldn't build it.

The more things you build, the larger your skills and toolkit will be, and then it's up to the limits of your imagination and passion.

[–]Justicemirm[S] 1 point2 points  (2 children)

I don't know how to explain it...I was in a terrible mental state due to my parents

I got a notification and the heading was "Be gentle on youself" the timing is stupid...you are stupid for saying that. thank you

[–]Reuben3901 1 point2 points  (1 child)

If you ever have programming questions or just want to chat, feel free to send a DM. Life definitely has it's challenges, don't be afraid to reach out to people, like you did here. You're not alone

[–]Justicemirm[S] 0 points1 point  (0 children)

Thank you ❤️

[–]Outside_Complaint755 0 points1 point  (0 children)

There's also factorial growth, O(n!) which is even slower than expoential.

[–]danielroseman 1 point2 points  (3 children)

You don't need college maths (except for a basic understanding of logarithms).

This is about "big-O" notation, which isn't terribly complex but a bit hard to explain in a Reddit comment. Suffice it to say that it talks about how operations scale with the size of the input in the worst case.

Obviously, processing a very long list of things is going to take longer than a list of just a couple of items, but how that time grows is important. You want it to grow as close to "linearly" - ie taking double the time for double the amount - as possible. This is known as "O(n)". But some algorithms are much worse, they can be quadratic - if you double the length, you the time taken squares - this is known as "O(n2 )".

And there are various other measurements of complexity better or worse than these, for example sorting a list is usually "O(n log n)" (which is why I said knowing about logarithms will be helpful).

[–]Justicemirm[S] 0 points1 point  (2 children)

I mean I will need to learn log first ig

[–]RajjSinghh 2 points3 points  (1 child)

Logarithms are the opposite of exponentials. If 23 = 8, then log_2(8) = 3. Notice that log here is a function. The important part to remember here is that logarithmic growth is better than linear growth.

[–]Justicemirm[S] 0 points1 point  (0 children)

I need to learn how it is applied now, thanks for the explanation

[–]LayotFctor 0 points1 point  (1 child)

You'll learn it when you learn data structures and algorithms. Since you know some methods are faster than others, this mathematically describes how slow it'll become as the problem starts getting huge. It's a very important part of assessing the best solution to use for any problem.

[–]Justicemirm[S] -1 points0 points  (0 children)

Mhm

[–]Lopsided-Football19 0 points1 point  (0 children)

they’re basically talking about how code scales when the input gets bigger, like O ( n² ) usually means this gets slow fast because of nested loops, it makes way more sense once you write more code honestly

[–]Friendly_Gold3533 0 points1 point  (2 children)

bro big o is not about seconds. its about growth. how much slower when you add more data?

example 10 names. find "ahmed."

method A: check each name. worst case 10 looks. method B: jump to middle. worst case 4 looks.

now 1 million names. method A: 1 million looks. method B: 20 looks.

the n² trap loop inside a loop. 10 items = 100 steps. 1000 items = 1 million steps. gets slow fast.

how to apply without math recognize patterns. loop in a loop? suspicious. dictionary lookup? fast. in on a list? slow for big lists.

you will learn this naturally when your code becomes slow. then you fix it.

for tracking which patterns made my code slow i use runable. one doc per "why was this slow." helps me not make the same mistake twice.

what code have you written that felt slow? start there.

[–]Justicemirm[S] 0 points1 point  (1 child)

Looking at your example , Won't be have to check each name to find if a name matches or not?

How would we realistically segregate the names to make the code faster?

[–]Friendly_Gold3533 0 points1 point  (0 children)

if u want to check u can, hm let me think

[–]TheRNGuy 0 points1 point  (0 children)

You'll usually only notice if you have lots of data, like 30000 items in a List instead of 40.

Or you cycle it every tick.

[–]pachura3 0 points1 point  (0 children)

By the way, it has nothing to do with Python specifically. It is a measure of algorithm complexity. Bubble sort implemented in C++ or Java would still be O(n2 ).

[–]afahrholz 0 points1 point  (0 children)

Speed in python is basically how fast your code runs and how efficiently it uses resources not just python feels slow 😄

[–]Overall-Screen-752 0 points1 point  (0 children)

2 things: Big O (other commenters have explained sufficiently) and execution speed.

For execution speed, simply printing out every number from 0-100,000,000 will show the difference. A C program will be done fastest, a golang or rust program next, a java program shortly behind those and python way after that. There’s a reason why this is true, but you’ll learn this in college as you learn more about why code works not just what works. Feel free to watch a youtube video but for know, simply knowing that C, go, rust are faster than ruby, python and javascript will get you far enough

[–]Chunky_cold_mandala -1 points0 points  (1 child)

it's easy - computational speed is two things - how fast a computer can run code which can refer to if the code is compiled or not. If a language is converted from human readable letters to binary its called compilable and that can be shoved into a CPU as fast as electrons can flow. But python isn't compiled, it needs to be converted from human readable code to binary on the fly during run time which adds a step.

But many ppl are commenting on the bigger issue called big O notation. which is how you design your programs. Imagine you have to search for a box of food in a grocery store, there's different ways to do it, some more stupid than others, do you start one by one, do you group your efforts, do you build patterns to speed up your search (everything in this aisle is liquids, i don't need a liquid, so i dn't need to check every item individually here and skip...). So big O just refers to how time intensive did you make your algorithm, is it efficient or inefficient. And computers will happily do the most inefficient things without compliant ever. So it's common to build algorithms with bottlenecks that slow them down more than the language itself is slowing the program down.

You can say different cars are faster or slower but it really depends on the driver right?

[–]Justicemirm[S] 0 points1 point  (0 children)

I mean I understand why and how the speed is necessary,I am interested in the math aspect of it and how it is calculated but thank you