view profile.c @ 35:3b92a4b17049 default tip

fix for macosx Yosemite
author Nozomi
date Thu, 19 May 2016 18:18:34 +0900
parents 972a7f233b23
children
line wrap: on
line source

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/time.h>
#include<unistd.h>
#include"profile.h"

static unsigned long timeprof_overhead;

#define timeprof_get_time(t) {\
    struct timeval t0;\
    gettimeofday(&t0,NULL);\
    (t).sec=t0.tv_sec;\
    (t).usec=t0.tv_usec;}

void timeprof_begin(Timeprof t)
{
    timeprof_get_time(t->begin);
}

int timeprof_end(Timeprof t)
{
    unsigned int etime;
    timeprof_get_time(t->end);
    if (t->begin.usec > t->end.usec) {
	t->end.usec += 1000000;
	t->end.sec--;
    }
    etime = ((t->end.sec - t->begin.sec) * 1000000 +
	     (t->end.usec - t->begin.usec)) - timeprof_overhead;
    if (etime > t->peak) {
	t->peak = etime;
    }
    if (t->average > 0) {
	t->average += etime;
	t->average /= 2;
    } else {
	t->average = etime;
    }
    return etime;
}

void timeprof_sprint(char *s, const char *profname, Timeprof t)
{
    sprintf(s, "%s: average:%dusec, peak:%dusec", profname, t->average,
	    t->peak);
}

void timeprof_init()
{
    struct time_profile t;
    timeprof_begin(&t);
    timeprof_overhead = timeprof_end(&t);
}

Timeprof timeprof_new()
{
    Timeprof t = (Timeprof) malloc(sizeof(struct time_profile));
    memset(t, 0, sizeof(struct time_profile));
    return t;
}