SDSU CS660 Combinatorial Algorithms
Fall Semester, 1996
Timing Code

[To Lecture Notes Index]
San Diego State University -- This page last updated Monday, 09 September, 1996
----------

Contents of Timing Code

  1. References
  2. Timing Analysis
    1. Handling Measurement Errors1
    2. Estimating Complexity from Timing Results
    3. Mathematical Analysis and Timing Code

                                                Timing Code Slide # 1

References


Introduction to Mathematical Statistics, 3rd Edition, Hogg, Craig, 1970, Chapter 6


                                                Timing Code Slide # 2

Timing Analysis

Timing in C on Rohan


main()
{
int k, iterations;
for (iterations = 0; iterations < 50; iterations++)
{
start();
/* start the timer */
for (k = 0; k < 2000000; k++)
/* do some work */
k = k;
stop();
/* stop the timer */
printf("Time taken: %ld\n", report());
};
}
Result on Rohan

Time Frequency Occurred
30 2
31 2
32 9
33 10
34 11
35 9
36 5
37 1
39 1
                                                Timing Code Slide # 3
Source for Timing C Code on Rohan

#include <stdio.h>
#include <sys/times.h>
#include <limits.h>

static struct tms _start; /* Stores the starting time*/
static struct tms _stop; /* Stores the ending time*/


int start()
{
times(&_start);
}

int stop()
{
times(&_stop);
}

unsigned long report()
{
return _stop.tms_utime - _start.tms_utime;
}

main()
{
int k, iterations;
for (iterations = 0; iterations < 50; iterations++)
{
start();
/* start the timer */
for (k = 0; k < 2000000; k++)
/* do some work */
k = k;
stop();
/* stop the timer */
printf("Time taken: %ld\n", report());
};
}
                                                Timing Code Slide # 4

Handling Measurement Errors[1]


Repeat a measurement n times
Let the measurements be labeled

Let and

The confidence interval for the true measurement is[2]:

The value of t determine the probability the measurement is in the interval

When n >= 50
Probability value of t
50%80%90%95%99%
0.671.281.641.962.58
In Example

, s*s = 3.15, s = 1.78 selecting t = 1.96 we get

95% confidence interval is (33.20, 34.20) for actual loop time
                                                Timing Code Slide # 5
Student t table - When n < 50
r = n-190%95%99%
16.31412.70663.657
22.9204.3039.925
32.3533.1825.841
42.1322.7764.604
52.0152.5714.032
61.9432.4473.707
71.8952.3653.499
81.8602.3063.355
91.8332.2623.250
101.8122.2283.169
201.7252.0862.845
301.6972.0422.750

                                                Timing Code Slide # 6

Estimating Complexity from Timing Results

Fun with Functions

Let f(n) = 3n*n + 4n + 5 and g(n) = 3n*n


Fact: g(n) is an approximation of f(n)


Notation: f(n) = g(n) +

nf(n)g(n)% error
112375.00%
1034530013.04%
20128512006.61%
30282527004.42%
40496548003.32%
50770575002.66%
6011045108002.22%
7014985147001.90%
8019525192001.66%
9024665243001.48%
10030405300001.33%
2001208051200000.67%
3002712052700000.44%

                                                Timing Code Slide # 7
Eyeballing Complexity

Let then
Timing Results
NBubbleInsertion
10011
20053
4001911
8007942
1600317166

                                                Timing Code Slide # 8
Plotting Complexity
Cubic or Quadratic[3]?

                                                Timing Code Slide # 9
Plotting Complexity
Engineers Method (Modified)

Let then

Let b = 2 and then


                                                Timing Code Slide # 10
Plotting Complexity
Transform the Axis

Let and (or ) then:

g(J) = f( ) = a( )k = aJ

So g(J) is linear!

                                                Timing Code Slide # 11
Example
nf(n) =5n*n+n + 3J=n*n
191
10513100
202023400
304533900
4080431600
50125532500
60180633600

                                                Timing Code Slide # 12
Which is Quadratic?




                                                Timing Code Slide # 13

Mathematical Analysis and Timing Code



Bubble sort worst case is ( n*n )

Complexity is an*n + bn + c

Timing Results Worst Case
NBubble Sort
40020
50031
60045
70061
80079

Least Squares fit of data to an*n + bn + c

Bubble sort worst case is 0.0001143n*n + 0.01084n - 2.738

Predicted vs. Actual Time for Bubble Sort
NActualPredicted% Error
90010599.6015.14%
1000124122.4021.29%
1100149147.4891.01%
2000496476.1424.00%
2400713681.6464.40%


----------