# Thread: How to create a mini-benchmarking program?

1. ## How to create a mini-benchmarking program?

I have an assignment where I need to make a benchmark program to test the performance of any processor with two sorting algorithms (an iterative one and a recursive one). The thing is my teacher told me I had to create three different programs (that is, 3 .c files), two with each sorting algorithm (both of them have to read integers from a text file separated with \n's and write the same numbers to another text file but sorted), and a benchmarking program. In the benchmark program I need to calculate the MIPs (million instructions per second) with the formula MIPs = NI/T*10^6, where NI is the number of instructions and T is the time required to execute those instructions. I have to be able to estimate the time each algorithm will take on any processor by calculating its MIPs and then solving that equation for T, like EstimatedTime = NI/MIPs*10^6. My question is... how exactly do I measure the execution time of a .c file from another .c file? I have never done something like that. I mean, I guess I can use the TIME functions in C and measure the time to execute X number of lines and stuff, but I can do that only if all 3 functions (2 sorting algorithms and 1 benchmark function) are in the same program. I don't even know how to start.

Oh and btw, I have to calculate the number of instructions by cross compiling the sorting algorithms from C to MIPS (the asm language) and counting how many instructions were used.

Any guidelines would be appreciated... I currently have these functions:

• writefile
• sorting algorithms

2. Are all three .c files separate programs, each with a main() ?

3. I don't actually know, I assumed they are, otherwise my teacher would have told me.
What's the practical difference for this? :0

4. Something like this might work (assuming *nix).
Code:
```#define _BSD_SOURCE 1
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int main(void) {
struct timespec ts1, ts2;
clock_gettime(CLOCK_REALTIME, &ts1);

pid_t pid = fork();
if (pid == (pid_t)-1) {
perror("fork");
exit(EXIT_FAILURE);
}

if (pid == 0) {  // child
execlp("./aprog", "aprog", "afile", (const char *)NULL);
perror("exec");
exit(EXIT_FAILURE);
}

// parent
int status = 0;
wait(&status);
clock_gettime(CLOCK_REALTIME, &ts2);
if (ts2.tv_nsec < ts1.tv_nsec) {
ts2.tv_nsec += 1000000000;
ts2.tv_sec--;
}
printf("%ld.%09ld\n", (long)(ts2.tv_sec - ts1.tv_sec),
ts2.tv_nsec - ts1.tv_nsec);

return 0;
}```
This is also timing the fork and exec, but I don't see a way around that.