C Program to two timing functions to measure process time

How to write a C Program to Two Timing Functions to Measure Process Time in C Programming Language ?


Solution:
/*C Program to two timing functions to measure process time*/

#include <sys/times.h>
#include <time.h>
#include <unistd.h>
#include <stdio.h>
#include "shell.h"

/* Storage for baseline times. */
static clock_t start_time;
static struct tms start_tms;

/* Save a baseline of user and system CPU times, plus wallclock time. */
void set_timer(void) {
// struct tms tmbuf;
/* Fill in code. */
start_time = times(&start_tms);
}


/* Get second set of times, and take delta to get wallclock time.  Display. */
void stop_timer(void) {
struct tms tmbuf;
clock_t end_time;
double ticks;

ticks = sysconf(_SC_CLK_TCK);

/* Get delta times and print them out.
*
* Fill in code. */

end_time = times(&tmbuf);

printf("Sys: %lf\tUser: %lf\tReal: %lf\n",
(double)(tmbuf.tms_stime - start_tms.tms_stime*0),
(double)(tmbuf.tms_utime - start_tms.tms_utime*0),
(double)(end_time - start_time) / ticks
);
}


Learn More :