SLOG # 11: Big O, back to the basics
Here comes efficiency—It’s time to be professional. Gone are the days of mindlessly slapping code together and only focusing on the fact that it worked—We were CSC148 students, and it was time to take that step into more effective and streamlined maturity.
After getting an introduction to efficiency last week, we got down to the nitty gritty this lecture. There’s two ways of measuring efficiency; first is by time, and second is by memory. However, we’re going to shove memory to the side a bit this week—Time was of the essence. How fast did it take a code to execute? And how could we time it?
This is where Big O comes in.
If you want to be a pro, you got to know the lingo. What in the world is Big O, a young, naïve, two weeks ago me may have asked. Present me can now answer with confidence—It’s a way of measuring how long an algorithm takes to run. O(1) has a constant run time, and O(n2) has a run time relative to n. What’s faster? Constant ( O(1) ) always is, and is considered to have the lowest runtime, while Factorials ( O(n!) ) are atrocious with getting anything done in an acceptable amount of time, the old men they are.
But Bread, you may be asking. “What about in the case the algorithm for function A is f(x) = 6000, and the algorithm for function B is g(x) = 2x? When x is 1, Function B, which is O(n), would be faster than Function A, which is O(1), right? Your logic doesn’t make sense!
Dear grasshoppers, please think again to the last lecture—When we’re calculating Big O, we’re always considering worst case and thinking about x in arbitrarily big numbers. Just let your n get up to, say, 300000. Then you can see that O(1) is much more efficient than O(n). This was repeated many times in these lectures, too. Key point, take note!
When we’re trying to find the Big O of a function, we want to focus on loops. They’re the one that determines n, and you want to pay special focus to them—the rest are minor. Ignore them, unless you’re trying to find out the specific algorithm for that function. But for Big O, we only look at the Dominant Term—which is the term that leads the algorithm; i.e, the term that grows the fastest. Loops tend to define these terms.
If a loop that iterates n times is nested inside a loop that also iterates n times, you multiply n x n, which gives us O( n2 ). If they’re not nested and follow each other, then you add—n + n, which gives us O(n).
Which makes me wonder—how are Big (O) s of recursive functions calculated? Does each recursion have an O () value or is the function evaluated as a whole? Also, what about if and elif functions, where there’s a for loop nested in one of the elifs but not in the rest? Where when that for loop is evaluated, Big O is n, but when it’s not, it’s O 1)? Worst case scenario rule says that we should consider the for loop and say O(n) in that case, but it’s a thought to think about.
Big O—it seems like such a simple concept, but in application, it was challenging and mind engaging. There were so many variables to think about, and I found myself getting answers wrong left and right. Practice makes perfect, however. Big O questions are fun, and I look forward to doing them – I just hope I don’t mess up on the Final Exam.
Looking back, I realized a bunch of things—like the theme of friendship and how that tied everything together into a – no, I’m just kidding. Maybe it’s not friendship, but this week’s lecture certainly tied everything into focus—everything we learned, until now, tied elegantly with Big O bow. I’m glad. I only knew iteration when I came in, and I dipped into recursion on the week 6 and 7 Slog— I thought that was the bomb when I first learned about it, but now I see it isn’t quite so—they both have their strengths and weaknesses.
It feels like I’m taking baby steps again. Big baby steps, but still.
The nervousness about the test and exams stay the same, though. Good luck, everyone!











