Last active
October 31, 2018 09:14
-
-
Save pintman/95def0e4a41fcd37b8e4c9413a7d4226 to your computer and use it in GitHub Desktop.
Linear, polynomial and exponential runtime analysis.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
WAIT_TIME = 0.001 # seconds | |
MAX = 10 | |
def do_something_time_comsuming(): | |
time.sleep(WAIT_TIME) | |
def linear(n): | |
for i in range(n): | |
do_something_time_comsuming() | |
def quadratic(n): | |
for _i in range(n*n): | |
do_something_time_comsuming() | |
def cubic(n): | |
for _i in range(n**3): | |
do_something_time_comsuming() | |
def exponential(n): | |
for i in range(3**n): | |
do_something_time_comsuming() | |
print("LINEAR") | |
for i in range(MAX): | |
start = time.time() | |
linear(i) | |
delta = time.time() -start | |
print(str(i) + ";" + str(round(delta, 4))) | |
print("QUADRATIC") | |
for i in range(MAX): | |
start = time.time() | |
quadratic(i) | |
delta = time.time() -start | |
print(str(i) + ";" + str(round(delta, 4))) | |
print("CUBIC") | |
for i in range(MAX): | |
start = time.time() | |
cubic(i) | |
delta = time.time() -start | |
print(str(i) + ";" + str(round(delta, 4))) | |
print("EXP") | |
for i in range(MAX): | |
start = time.time() | |
exponential(i) | |
delta = time.time() -start | |
print(str(i) + ";" + str(round(delta, 4))) | |
''' | |
Possible output | |
LINEAR | |
0;0.0 | |
1;0.0011 | |
2;0.0021 | |
3;0.0032 | |
4;0.0043 | |
5;0.0053 | |
6;0.0064 | |
7;0.0074 | |
8;0.0085 | |
9;0.0096 | |
QUADRATIC | |
0;0.0 | |
1;0.0011 | |
2;0.0043 | |
3;0.0095 | |
4;0.017 | |
5;0.0266 | |
6;0.0383 | |
7;0.052 | |
8;0.068 | |
9;0.0859 | |
CUBIC | |
0;0.0 | |
1;0.0011 | |
2;0.0085 | |
3;0.0288 | |
4;0.068 | |
5;0.1347 | |
6;0.2329 | |
7;0.3692 | |
8;0.5433 | |
9;0.7736 | |
EXP | |
0;0.0011 | |
1;0.0032 | |
2;0.0096 | |
3;0.0287 | |
4;0.086 | |
5;0.2585 | |
6;0.7779 | |
7;2.3645 | |
8;7.0943 | |
9;21.2798 | |
''' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment