The Next Look Into Efficient Coding

Calculating Big-O

Calculating the Best Way to Write Code

Paul Torres
Analytics Vidhya
Published in
6 min readFeb 1, 2021

--

Photo by Harley-Davidson on Unsplash

In my last article, I spoke about algorithms. You can read up on it here. In short, algorithms are just a method of doing something — anything really. However, as in all things, there are better and quicker ways to accomplish a goal. You can carry a crate down the street or you can use a cart. Both methods accomplish the goal but one is faster and more efficient. The same goes with coding.

When running code, the metric most people think about first is time.

…..How long is the code going to take?

In the end, that is what matters, right? Once we figure out how to accomplish a task, the only thing we want to tweak is how fast it gets the job done. Unfortunately, this is a red herring. And the reason for this is not all computers are equal. My laptop connected to an external monitor should not be expected to perform at the same rate as a tricked out desktop.

That is where Big-O notation comes into play. Big-O is not about how long a specific computer does a specific task but rather how much longer that task becomes for any computer when the input increases. O(n) is the order of increase for runtime for a block of code. As (n) grows larger, how…

--

--

Paul Torres
Analytics Vidhya

Data Scientist with a Physicist’s heart. Looking through numbers to tell a story that people will care about.