|
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 | |
|---|
| 7 | struct timeval begin; |
|---|
| 8 | struct timeval end; |
|---|
| 9 | double t1; |
|---|
| 10 | double t2; |
|---|
| 11 | int handle; |
|---|
| 12 | |
|---|
| 13 | int 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 | |
|---|
| 25 | int chrono_end() { |
|---|
| 26 | if (handle) { |
|---|
| 27 | return close(handle); |
|---|
| 28 | } |
|---|
| 29 | |
|---|
| 30 | return 0; |
|---|
| 31 | } |
|---|
| 32 | |
|---|
| 33 | void chrono_start() { |
|---|
| 34 | char mess[2048]; |
|---|
| 35 | |
|---|
| 36 | gettimeofday(&begin, NULL); |
|---|
| 37 | } |
|---|
| 38 | |
|---|
| 39 | long 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 | } |
|---|