Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Lab 1

Timing algorithms

How do you determine the running time of an algorithm?

Java includes methods for determining the current time to a high precision. Lets try to compute the runnig time of a method containning two nested loops For (Modify the number of iterations in each loop and see the effect on the running time)

long start_time; // The time the algorithm started
long end_time;   // The time the algorithm ended
Random rd = new Random();
start_time = System.currentTimeMillis();
int s=0;
for(int i=0; i<100000000;i++)
        s+=rd.nextInt(100)*rd.nextInt(100);
end_time = System.currentTimeMillis();
System.out.println("The algorithm took " + (end_time-start_time) + 
                " milliseconds.");
The algorithm took 6750 milliseconds.

Problems with wall-clock timing

  • Timing is highly dependent on the machine being used.

Does this mean that you shouldn’t use wall-clock timing?

Certainly not. It is a valuable tool in your toolbox.

Computing the GCD of two numbers

Consecutive integer checking algorithm: gcd(m,n)

  • Step 1 Assign the value of min{m,n} to t

int gcd1(int m, int n){
    if(m==0)
        return n;
    if(n==0)
        return m;
    int t = (m<n)? m:n;
    while (m%t !=0 || n%t != 0){
        t--;
    }
    return t;
}
gcd1(6,0);
6

Euclid’s algorithm:

  • Step 1 If n = 0, return m and stop; otherwise go to Step 2

int gcd2(int m, int n){
  if(m==0)
    return n;
  if(n==0)
    return m;
  int r ;
  while(m % n !=0){
    r= m % n;
    m=n;
    n=r;
  }
  return n;
}

Comparing the execution time of the two algorithms

long start_time; // The time the algorithm started
long end_time;   // The time the algorithm ended
start_time = System.currentTimeMillis();
gcd1(5000,14800);
end_time = System.currentTimeMillis();
System.out.println("The algorithm 1 took " + (end_time-start_time) + 
                " milliseconds.");
start_time = System.currentTimeMillis();
gcd2(5000,14800);
end_time = System.currentTimeMillis();
System.out.println("The algorithm 2 took " + (end_time-start_time) + 
                " milliseconds.")
The algorithm 1 took 41 milliseconds.
The algorithm 2 took 39 milliseconds.

Counting steps

Instead of measuring wall-clock time, we can augment our methods to count the number of steps they do. Obviously, counting every line executed in a program is difficult. Therefore, you will typically count the number of basic operations executed.

int gcd1(int m, int n){
    int count=0;
    if(m==0)
        return n;
    if(n==0)
        return m;
    int t = (m<n)? m:n;
    while (m%t !=0 || n%t != 0){
        count++;
        t--;
    }
    System.out.println("The number of divisions is : "+ count);
    return t;
}
int gcd2(int m, int n){
    int count=0;
    if(m==0)
      return n;
    if(n==0)
      return m;
    int r ;
    while(m % n !=0){
      count++;
      r= m % n;
      m=n;
      n=r;
    }
    System.out.println("The number of divisions is : "+ count);
    return n;
  }
  gcd1(5000,14800);
  gcd2(5000,14800);
The number of divisions is : 4800
The number of divisions is : 3
200