Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Calculating Big O

DZone's Guide to

Calculating Big O

Big O seems to be everywhere. But what is Big O? What is the point of knowing Big O? Will Big O calculation improve my coding skills? Is it even worth trying?

· Performance Zone
Free Resource

Big O simple means Big Order. "Big" is something of considerable importance or seriousness, and "order" is the power with which your values changes.

Big O can also be considered a modern way of calculating the performance of different data sizes. With a good and solid foundation in Big O, your programming or software life becomes easy. Big O is used in DSAs (data structures and algorithms).

You need Big O to ensure that your software has very good performance with both small and big data. Imagine that you are writing a small search application to look for one million pieces of sorted data stored in one hundred machines that are connected to a single database — that means one thousand pieces of data on each machine. How can you find the data that you want?

Binary and linear will do that for you, but which one should you go for? That is where Big O calculation comes in. Remember your high school logarithms? Log 10 base 10 = 1 …since all the data are connected to 100 machines to a single database, linear search (looking for the data one by one until you see what you are looking for) could be a nice approach, but it would be time-consuming.

Here comes the Big O calculator! As a smart person, you should be thinking of going to the machine with the data that you are looking for, i.e using the binary search method and checking if the middle data match what you are looking for.

Log 10 of 1,000,000 = 6 into log 2 that will be 19.932 ~ 20 — meaning that you only have to look for any data you are looking for in 1,000,000 data in 20 times, maximum. The question is, how long will it take me to get what I'm looking for? You know that smartest way to get the data you looking for. It’s simple. Take the following: 

T = time and K = constant.

T = K (time is the constant of any specified algorithm).

T = K Log 10 (N) into Log 2 (N). So, if Log 10 (1,000,000) into Base 2 is 20, T = 20K and K = T/20.

Take K = Log 2 (N)/N.

K = 20/1,000,000; K = 20 (0.00002); K = 0.0004.

T = 0.0004 hour; T = 0.0004 * 60; T = 0.024 minute; T = 0.024 * 60; T = 1.44 seconds.

Isn’t that smart?

Now, you can choose the best solution.

There are many ways that Big O can be calculated. Some of them are:

  • Invariant: Conditions that remain unchanged as the algorithm proceeds.

  • N loop: Whenever you see nested loops such as those in this algorithm, you can suspect that an algorithm runs in O(N2) time. The outer loop executes N times and the inner loop executes N (or perhaps N divided by some constant) times for each cycle of the outer loop. This means you’re doing something approximately N*N or N2 times.

Lastly, memory space also matters a lot.

That's it. Thanks for reading!

Topics:
big o

Published at DZone with permission of Amuda Adeolu. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}