Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Objective:
The Objective of this Lab is to measure the execution times of different algorithms using C
programming language. A simple piece of code will be used to measure the execution time of an
algorithm with sufficient accuracy. Measuring running time is useful especially in the design and
analysis of Algorithms.
Introduction:
To measure the running time of a piece of code, we need to find the number of clock cycles used by
the processor for processing the instructions constituting the code. For example, if the code utilizes
1000 clock cycles for execution and there are 100 clock cycles per second, the execution time of the
code will be 10s.
Method 1 – Clock
Make a new project on MS Visual studio. Start by making an empty project and then add .c file to the
project and enter the following code in it.
The Description of datatype clock_t and clock() function is given after the code.
Sample Code:
#include <stdio.h>
#include <time.h>
int main() {
clock_t start, end;
int i, j;
int outer_loop = 2100;
int inner_loop = 2000;
double run_time;
// Any code whose execution time is to be checked comes between the start and
the end time for example:
getchar();
return 0;
}
clock_t:
Alias of a fundamental arithmetic type capable of representing clock tick counts. Clock ticks are the
unit of time of a constant but system-specific length, as those returned by function clock().
clock() function:
The function returns the processor time consumed by the program. The value returned is of type
clock_t (clock ticks).
CLOCKS_PER_SEC:
Note:
Compiling the same program for different times might give you a different value. This is due to the
fact that your processor is processing many background applications apart from executing your
program. Due to this the value returned by the above code might be different sometimes.
Compile the same program for five times. Fill out the table below by noting down the execution time
for each run and compute the average.
Vary the outer and inner loop indexes in the code above (pick any value). Observe the change in
execution time and fill the following table:
Another way of measuring execution time is using Query Performance Counter. The sample code is
given below.
#include <stdio.h>
#include <Windows.h>
#include <time.h>
int main() {
int i, j;
int outer_loop = 2100;
int inner_loop = 2000;
double run_time;
QueryPerformanceFrequency(&frequency);
QueryPerformanceCounter(&start_time);
// Any code whose execution time is to be checked comes between the start and
the end time
QueryPerformanceCounter(&end_time);
getchar();
return 0;
QueryPeformanceCounter() this function retrieves the current value of the performance counter. It
gives the number of ticks.
The code for the maximum sub sequence sum is given at the end of the document.
Task
#include <stdio.h>
#define LinearAlgorithm
}
}
return MaxSum;
}
#endif
}
return MaxSum;
}
#endif
MaxLeftBorderSum = 0;
LeftBorderSum = 0;
MaxRightBorderSum = 0;
RightBorderSum = 0;
#endif
return MaxSum;
}
#endif