Nested loops are faster!

While solving a problem on hackerrank, I had to use for nested loops in problem related to 2D array.  Then, I realised I should use single loop rather than nested and then, I tried myself both the approaches to check which approach is better and faster.

Every time for similar iterations, nested loops were faster than the single loops.  Below is the python code to verify yourself.
The following code takes the inputs 10, 20, 50, 100 and outputs the time required by both the approaches.

Python Code:


import time

def loopinloop(n):
    StartTime = time.time()
    for i in range(0, n):
        for j in range(0, n):
            # Do nothing
            continue;
    return (time.time() - StartTime);

def loopalone(n):
    StartTime = time.time()
    for i in range(0, n**2):
        continue;
    return (time.time() - StartTime);

if __name__ == "__main__":
    number = [10, 20, 50, 100];
    withinloops = [loopinloop(num) for num in number];
    oneloop = [loopalone(num) for num in number];
    print("withinloops = {0}".format(withinloops))
    print("one loop = {0}".format(oneloop))

Output:

withinloops = [1.0013580322265625e-05, 1.9073486328125e-05, 7.987022399902344e-05, 0.0002682209014892578]
one loop = [4.0531158447265625e-06, 1.1920928955078125e-05, 9.298324584960938e-05, 0.0003771781921386719]


As the above code and its output suggests, nested loops are faster which is quite a contradiction in itself i.e. O(n^2) is faster than O(n).  But after searching a little I found this Stackoverflow answer.

Thus, single loop is not always faster than nested loops.  It depends on the statements it is executing.  Hope this helps :)

Till then,

To Infinity and Beyond!

Everybody, Somebody, Anybody, Nobody!

During studying Communication Skills for my college exams, I found an amazing story which I would like to share.

Everybody, Somebody, Anybody, Nobody!

- A Communication Gap

This is a story about four people Everybody, Somebody, Anybody, and Nobody.  There was an important job to be done and Everybody was sure that Somebody would do it.  Anybody could have done it, but Nobody did it.  Somebody got angry about that, because it was Everybody's job.  Everybody thought Anybody could do it, but Nobody realized that Everybody wouldn't do it.  It ended up that Everybody blamed Somebody, when Nobody did what Anybody could have done!


Conclusion:  This story is far deeper than it might look and is best describe in team collaborative projects.  If any of you find difficulty understanding, please Comment. (It is rather difficult for me to explain it all over here)

Boot Loader vs BIOS!

If you guys have read my previous blog on LibreBoot, you guys might know that I have started to work on LibreBoot and here is something I like to share what I learn along the way on BIOS and boot-loader.

BIOS

BIOS is short for Basic Input Output System and is a very critical part of any computer system and it is very first part which powers up your system.

 When the system is powered on, a firmware loaded in a ROM(Read Only Memory) chip is executed and is known as BIOS.  This firmware takes care of your initials drivers required by your system for the keyboards and mouse to interact. Once the BIOS firmware is loaded, the process of booting goes to boot-loader.

Boot Loader

Booting takes place in two steps.  BIOS checks all the storage devices connected to the system and if the boot loader is present or not.

Boot Loader then in first step, checks for the master boot partition, aka MBR and verifies whether it is working or not i.e. not corrupted.

Then, in second step, boot loader points to the storage partition which stores the operating system for our system and is referenced by the MBR, whether it be windows or any linux based distribution.

In multiboot systems, MBR has the entry for all the storage partitions for all the operating system, and thus boot loader provides user the option to choose the operating system, s/he wants to use.


Note: Since I am learning these concepts on bootloader, bios and kernel.  So use this material on your risk.  I wont be responsible if your system crashes. :p








Moving!

It's been a long time, since I published anything here, but that doesn't mean I stop writing, I kept writing everyday, just didn...