root/tags/5.4.pre05/wxis_src/chronometer.c

Revision 1, 1.3 kB (checked in by heitor.barbieri, 4 years ago)

Criação do svn para Cisis.

Line 
1#include <sys/time.h>
2#include <sys/types.h>
3#include <sys/stat.h>
4#include <fcntl.h>
5#include <stdio.h>
6
7struct timeval begin;
8struct timeval end;
9double t1;
10double t2;
11int handle;
12
13int chrono_init(char * fname) {
14    if (fname) {
15        handle = open(fname, O_CREAT|O_APPEND|O_WRONLY,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
16        if (handle == -1) {
17            printf("chrono_init/file open error\n");
18            return 0;
19        }
20    }
21
22    return 1;
23}
24
25int chrono_end() {
26    if (handle) {
27        return close(handle);
28    }
29
30    return 0;
31}
32
33void chrono_start() {
34    char mess[2048];
35
36    gettimeofday(&begin, NULL);
37}
38
39long chrono_stop(char * message) {
40    long dif;
41    char mess[2048];
42
43    gettimeofday(&end, NULL);
44
45    dif = (long)(((end.tv_sec - begin.tv_sec) * 1000000) + (end.tv_usec - begin.tv_usec))/1000;
46
47//printf("debug sec=%ld msec=%ld\n", (long)((end.tv_sec - begin.tv_sec) * 1000000), (long)(end.tv_usec - begin.tv_usec));
48    if (!message) {
49        message = "";
50    }
51    sprintf(mess, "%s[%ld]\n", message, dif);
52
53    begin.tv_sec = end.tv_sec;
54    begin.tv_usec = end.tv_usec;
55
56    if (handle) {
57        if (write(handle, mess, strlen(mess)) == 1) {
58            printf("chrono_stop/write file error\n");
59            return 0;
60        }
61    } else {
62        printf(mess);
63    }
64
65//printf(mess);
66
67    return dif;
68}
Note: See TracBrowser for help on using the browser.