In computer science, the time complexity is the computational complexity that describes the amount of time it takes to run an algorithm. Time complexity is commonly estimated by counting the number of elementary operations performed by the algorithm, supposing that each elementary operation takes a fixed amount of time to perform. Thus, the amount of time taken and the number of elementary operations performed by the algorithm are taken to differ by at most a constant factor.
My little area to play around with remaking Ruby's methods in an attempt to understand the complexity behind them.
Using the pi_charts
and sinatra
gems to build graphs representing the processing time of the methods in order to be able to tweak them.
Usage:
ruby lib/build_graphs.rb
This will start a sinatra session. Open a new tab on your browser and in the address bar type:
localhost:4567
Give it a minute to crunch the numbers. A graph should appear on your screen.
I've also added the memory_checker
gem to compare processing speed to memory usage on a small vs large data set.
Usage:
Access the file and uncomment the method you want to test. Select the data range (1000 or 100 000), then save the file and run:
ruby lib/memory_checker.rb
from the command line.
Memory values will be printed to the terminal. You want to look at the top for:
Total allocated:
A simple timer class made to time the amount of time it takes for a block of code to be completed.
Usage:
Timer.time { [1, 2, 3, 4, 5].my_reverse }
p Timer.elapsedTime
=> 1.9964e-05
A simple shuffle system that computates a random number between 1 and the legth of the input, extracts that then loops until the length of the input is 0. The main impliment is made using array. The string and integer versions are broken into an array then has the array algorithm is parsed on them. The system can be made better, this is the basic version.
Usage:
[1, 2, 3, 4, 5].my_shuffle
=> [4, 5, 2, 3, 1]
Custom reverse method. Can reverse strings, integers and arrays.
Usage:
[1, 2, 3, 4, 5].my_reverse
=> [5, 4, 3, 2, 1]
Takes a string of words or characters, sorts it into values then displays the 2 most occuring.
Usage:
'a b c d a c d a'.two_most
=> "["a", 3], ["d", 2]"
Given a string of words, it returns the duplicates.
Usage:
'the quick brown fox jumps over the lazy dog'.my_duplicates
=> "the"
Description | Illustrated image |
---|---|
In mathematics, the Fibonacci numbers form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. | ![]() |
Usage:
8.my_fib
=> [0, 1, 1, 2, 3, 5, 8, 13, 21]
Custom sorting methods. Sorting options; my_bubble_sort, my_quick_sort, my_merge_sort, my_selection_sort. Can currently only sort arrays.
Below results are generated using shuffled data between 500 - 100 000 in length. Anything over that takes way too long for my processor to handle (Tested on 6th gen Intel i5 (4.2gHz), 16Gig Ram, 2 x AMD Radeon RX580 nitro GPUs running Kali Linux)
How many ways can you arrange a deck of cards? - Yannay Khaikin
The Hair Algorithm - Computerphile
Sorting Secret - Computerphile
Getting Sorted & Big O Notation - Computerphile
Comparing sorting algorithms using animations
TODO:
- Make selection sort more efficient
Insertion sort- Shell sort
- Comb sort
- Intro sort
- Tree sort