`
Figuring out how complex the program from complexity point of view can be helpful, but it doesn’t give the whole picture. It’s like if your food delivery guy said, ‘Congrats, we delivered your order in O(nlogn) >time!’ It wouldn’t really make sense to the customer or stakeholders, would it?”
These metrics are key to any application’s success. In today’s lecture, we’ll write a program to calculate the actual time and space it uses.
Program that measures the execution time of another program. We’ll use the clock()
function from the <time.h>
library to capture the start and end times of the program execution. Here’s an example program:
#include <stdio.h>
#include <time.h>
int main() {
clock_t start_time, end_time;
double cpu_time_used;
// Record the start time
start_time = clock();
// Place your code to measure execution time here
// For example, let's simulate some computation
int sum = 0;
for (int i = 1; i <= 1000000; ++i) {
sum += i;
}
// Record the end time
end_time = clock();
// Calculate the CPU time used in seconds
cpu_time_used = ((double) (end_time - start_time)) / CLOCKS_PER_SEC;
printf("Sum: %d\n", sum);
printf("CPU Time Used: %f seconds\n", cpu_time_used);
return 0;
}
In this example, I’ve included a simple loop that computes the sum of numbers from 1 to 1,000,000. You can replace this part with your own program that you want to measure the execution time for. The clock()
function returns the number of clock ticks used by the program, and we convert it to seconds using the CLOCKS_PER_SEC
constant.
Remember that this approach measures the CPU time used by the program, which might include time when your program is not actively running due to multitasking and other factors. If you want to measure the actual elapsed time, you might need to look into system-specific functions, such as gettimeofday()
on Unix-like systems or QueryPerformanceCounter()
on Windows.
Also, keep in mind that modern operating systems and hardware can introduce variability in timing due to various optimizations and scheduling, so the accuracy of the measurement might not be perfect for very short durations.
**Here is a recommened YouTube Video to understand the Clock Speed in the CPU :
To measure the memory taken by a program in C, you can use platform-specific functions. Here’s an example using the getrusage()
function on Unix-like systems (Linux, macOS, etc.) to measure the memory usage of a program:
#include <stdio.h>
#include <sys/resource.h>
int main() {
// Record the start memory usage
struct rusage usage_start;
getrusage(RUSAGE_SELF, &usage_start);
// Place your code to measure memory usage here
// For example, let's allocate some memory
int *array = (int *)malloc(1000000 * sizeof(int));
// Record the end memory usage
struct rusage usage_end;
getrusage(RUSAGE_SELF, &usage_end);
// Calculate memory usage in kilobytes
long memory_used = usage_end.ru_maxrss - usage_start.ru_maxrss;
printf("Memory Used: %ld KB\n", memory_used);
// Free the allocated memory
free(array);
return 0;
}
In this example, we use the getrusage()
function to get resource usage statistics before and after the code you want to measure. The ru_maxrss field
in the struct rusage contains the maximum resident set size in kilobytes, which represents the amount of memory used by the program. Keep in mind that this measures the maximum memory used throughout the program’s execution.
Remember that different platforms may have different ways of measuring memory usage, and the values you obtain might not be an exact representation of the memory your program is using due to various factors like memory allocation granularity and operating system optimizations.