diff profile.c @ 0:435ac1cdb64e

create task dandy directry.
author koba <koba@cr.ie.u-ryukyu.ac.jp>
date Sat, 11 Dec 2010 21:25:28 +0900
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/profile.c	Sat Dec 11 21:25:28 2010 +0900
@@ -0,0 +1,61 @@
+#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;
+}