Applying BigO
We use the O(.) running time to estimate the actual running time of a program.
The following has Maple commands intermixed with regular text.
- We analyzed squares.py and determined that it's 'ideal' running time is
T(n)=1+1+8n+1+n+7n^2+1+1
.
- Exercise: Prove that
T(n)=1+1+8n+1+n+7n^2+1+1
is in O(n^2)
.
- We let
t(n)
be the number of milliseconds that squares takes to solve
a problem of size n
(that is, with n
points).
Based on our above analysis, we conclude that (using Maple notation)
t := n -> a2*n^2+a1*n+a0
where a2, a1, a0
are to be determined.
- We ran squares.py on a few inputs generated by writePoints.py (see squares.data)
points:=[
[1000,78.9041519165],
[3000,671.78606987],
[5000,1709.86890793],
[10000,6951.59602165],
[20000,26423.099041]
]
That is,
t(1000)=78.9041519165
t(3000)=671.78606987
t(5000)=1709.86890793
t(10000)=6951.59602165
t(20000)=26423.099041
- We solve the linear system...
solve({t(1000)=78.9041519165, t(3000)=671.78606987, t(5000)=1709.86890793}, {a2, a1, a0})
to get
{a2 = 0.5565011501e-4, a1 = 0.7384049892e-1, a0 = -50.58646202}
- Now if we set
a2:= 0.5565011501e-4;
a1:= 0.7384049892e-1;
a0:= -50.58646202;
we can evaluate t for other input sizes...
t(1000)= 78.90415188
t(3000)= 671.7860699
t(5000)= 1709.868908
t(10000)= 6252.830028
t(20000)= 23686.26952
t(40000)= 91943.21752
or plot them both together...(see plotting with maple)
with(plots, textplot, display);
plot1 := plot(points);
plot2 := plot(t, 1000 .. 40000);
display(plot1, plot2);