9 #include <sys/resource.h>
13 static const char *progname = "timeit";
15 static void set_progname(const char *prog)
19 p = strrchr(prog, '/');
20 progname = p ? p + 1 : progname;
23 static void lose(const char *msg, ...)
28 fprintf(stderr, "%s: ", progname);
29 vfprintf(stderr, msg, ap);
35 static double timeval_to_float(const struct timeval *tv)
36 { return (tv->tv_sec + tv->tv_usec*1e-6); }
38 int main(int argc, char *argv[])
41 struct timeval t0, t1, t;
45 set_progname(argv[0]);
47 kid = fork(); if (kid < 0) lose("fork failed: %s", strerror(errno));
49 execvp(argv[1], argv + 1);
50 lose("exec (`%s') failed: %s", argv[1], strerror(errno));
52 if (wait4(kid, &st, 0, &ru) < 0) lose("wait failed: %s", strerror(errno));
56 lose("program killed by signal %d\n", WTERMSIG(st));
57 else if (WIFEXITED(st))
58 lose("program failed with status %d\n", WEXITSTATUS(st));
60 lose("program exited with incomprehensible status 0x%04x\n", st);
63 if (t0.tv_usec > t1.tv_usec) {
64 t.tv_sec = t1.tv_sec - t0.tv_sec - 1;
65 t.tv_usec = t1.tv_usec + 1000000 - t0.tv_usec;
67 t.tv_sec = t1.tv_sec - t0.tv_sec;
68 t.tv_usec = t1.tv_usec - t0.tv_usec;
71 for (i = 1; i < argc; i++) {
72 if (i > 1) fputc(' ', stderr);
73 fputs(argv[i], stderr);
75 fprintf(stderr, ": elapsed = %.4fs; user = %.4fs; system = %.4fs\n",
77 timeval_to_float(&ru.ru_utime),
78 timeval_to_float(&ru.ru_stime));