Code:
#include <stdio.h>
#include <stdlib.h>
#include <sys/times.h>
clock_t get_tms(struct tms *dest)
{
clock_t whatever=times(dest);
return whatever;
}
char *fmt(char *dest, struct tms *p, clock_t elapsed)
{
int sdecimal=p->tms_stime % CLK_TCK;
int ssec=p->tms_stime / CLK_TCK;
int udecimal=p->tms_utime % CLK_TCK;
int usec=p->tms_utime / CLK_TCK;
int edecimal=elapsed % CLK_TCK;
int esec= elapsed / CLK_TCK;
sprintf(dest, "Elapsed:%3d.%02d User:%3d.%02d System:%3d.%02d",
esec, edecimal, usec, udecimal, ssec, sdecimal);
return dest;
}
void disp(int bufsiz, int cnt, struct tms *start, struct tms *end, clock_t elapsed)
{
struct tms mytms={0,0,0,0};
char tmp[256]={0x0};
mytms.tms_utime=end->tms_utime - start->tms_utime;
mytms.tms_stime=end->tms_stime - start->tms_stime;
printf("Buffer: %6d Reads: %10d ", bufsiz, cnt);
printf("%s\n", fmt(tmp, &mytms, elapsed ));
}
int main(int argc, char **argv)
{
int bufsz=atoi(argv[1]);
char *buf=NULL;
char *tmp=NULL;
clock_t elapsed=0;
int cnt=0;
FILE *in=fopen("test.dat","r");
struct tms start={0,0,0,0};
struct tms end={0,0,0,0};
buf=malloc(bufsz+1);
tmp=malloc(bufsz+2);
setvbuf(in, buf, _IOFBF, bufsz);
elapsed=get_tms(&start);
while(fread(tmp, bufsz, 1, in)==1)
cnt++;
elapsed=get_tms(&end) - elapsed;
disp(bufsz, cnt, &start, &end, elapsed);
fclose(in);
free(buf);
free(tmp);
return 0;
}