Member-only story

Comparing Running Times with Big O Notation

Big O Notation, Functions, Algorithms, DSA

Josephine Gyamera
2 min readFeb 25, 2024

Some time ago, I wrote about Big O notation. I mostly used pictures to show how fast different functions are. Now, I’ve created a special function based on the book “Introduction to Algorithms” by Thomas H. Cormen and others. This function helps us compare the time it takes for different functions to run, and we can see this comparison in different time units.

Let’s break down the code:

/**
* ---Comparison of running times---
* For each function f(n) and time t in the following table, determine the largest size n of a problem that can be solved in time t,
* assuming that the algorithm to solve the problem takes f(n) microseconds.
*/

/**
* 1s = 10^3ms
* 1min = 60s = 60(10^3)ms
* 1h = 60min = (60)(60)(10^3)ms
* 1d = 24hr = (24)(60)(60)(10^3)ms
* 1month = 30.44days= (30.44)(24)(60)(60)(10^3)ms
* 1y = 365.25days= (365.25)(24)(60)(60)(10^3)ms
* 1c = 100y= (100)(12)(30)(24)(60)(60)(10^3)ms
*/

const unitToMilliSecondsMap = {
"1s": 10 ** 3,
"1min": 60 * 10 ** 3,
"1hr": 60 * 60 * 10 ** 3,
"1day": 24 * 60 * 60 * 10 ** 3,
"1month": 30.44 * 24 * 60 * 60 * 10 ** 3,
"1year": 365.25 * 24 * 60 * 60 * 10 ** 3,
"1c": 100 * 365.25 * 24 * 60 * 60 * 10 ** 3,
};

function runningTimeForN(fn) {
let result = {};

for (let key in…

--

--

Josephine Gyamera
Josephine Gyamera

Written by Josephine Gyamera

Just learning cool Tech stuffs😎

No responses yet