One Pager Cheat Sheet
- With
Algorithm Complexity and Big O Notation
, developers can measure the performance of a program, in terms oftime
andspace
complexity, to determine the most effective algorithm to use. - Using Program 1 as an example, we can see the importance of ensuring that algorithms are designed to be as efficient as possible to maximize performance.
- We can use
%timeit
in a Jupyter Notebook or language interpreter to measure the performance of an algorithm. - The program took
600 ns
±230 ns
per loop,4 milliseconds
to call customPower, and337291 nanoseconds
in total. - The built-in
pow
function took600
nanoseconds to raise3
to the power4
, which is substantially less compared to program 2. - The execution time for Python, Javascript, and Java was
308 ns
,7 ms
, and1555964 ns
, respectively. - The efficient choice of algorithms can greatly improve user experience, and this is shown by
Program 2
being twice as fast asProgram 1
, taking only308
nanoseconds. - Big(O) Notation measures the time complexity of an algorithm based on the relationship between the number of inputs and the steps it takes to process them.
- The best complexity metric given is
O(log(n))
, as it takes logarithmic time in proportion to the input data. - The execution of a program with
Constant Complexity
remains the same, regardless of the input size. - The
display_first_cube
function has a constant algorithm complexity which does not scale with input. The
num_of_inputson the x-axis and the
stepson the y-axis plot out a straight line when graphed.
- A function or algorithm with
linear complexity
will require an equal increase in the number of steps required for execution as the number of units in itsinput
increases. - The
display_all_cubes
function has linear complexity and its execution time increases proportionally with the number of items in theitems
list. - The complexity of a function increases
quadratically
as the input size increases. - The
display_all_products
function in the code iterates throughn
items in an x n
loop and outputs agraph
showing the quadratic equation. - The space complexity of an algorithm can be determined by calculating the memory needed to store its inputs, expressed in terms of
Big(O) Notation
. - The space complexity of the
display_first_cube
isO(1)
(constant), while that of thedisplay_all_cubes
isO(n)
. - The complexity of an algorithm can be defined as its
worst case
scenario, which is optimized for the least ideal situation. - Sorting an array with consecutive elements is a best case scenario, requiring
O(n)
operations. - Measuring algorithm performance in terms of time taken and space consumed can be done through
Big(O) notation
. - No,
O(n)
time complexity does not necessarily mean that the algorithm will usen
nested loops.