File: prof.c

package info (click to toggle)
radare2 0.9.6-3.1%2Bdeb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 17,496 kB
  • ctags: 45,959
  • sloc: ansic: 240,999; sh: 3,645; makefile: 2,520; python: 1,212; asm: 312; ruby: 214; awk: 209; perl: 188; lisp: 169; java: 23; xml: 17; php: 6
file content (44 lines) | stat: -rw-r--r-- 1,322 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/* radare - LGPL - Copyright 2009-2012 - pancake */

#include "r_util.h"

typedef struct timeval tv;

// Subtract the 'tv' values begin from end, storing result in RESULT
// Return 1 if the difference is negative, otherwise 0.
static int timeval_subtract(tv *result, tv *end, tv *begin) {
	// Perform the carry for the later subtraction by updating Y
	if (end->tv_usec < begin->tv_usec) {
		int nsec = (begin->tv_usec - end->tv_usec) / 1000000 + 1;
		begin->tv_usec -= 1000000 * nsec;
		begin->tv_sec += nsec;
	}
	if (end->tv_usec - begin->tv_usec > 1000000) {
		int nsec = (end->tv_usec - begin->tv_usec) / 1000000;
		begin->tv_usec += 1000000 * nsec;
		begin->tv_sec -= nsec;
	}

	// Compute the time remaining to wait. 'tv_usec' is certainly positive.
	result->tv_sec = end->tv_sec - begin->tv_sec;
	result->tv_usec = end->tv_usec - begin->tv_usec;

	// Return 1 if result is negative
	return end->tv_sec < begin->tv_sec;
}

R_API void r_prof_start(struct r_prof_t *p) {
	tv *begin = &p->begin;
	p->result = 0.0;
	gettimeofday(begin, NULL);
}

R_API double r_prof_end(struct r_prof_t *p) {
	tv end, diff, *begin = &p->begin;
	int sign;
	gettimeofday (&end, NULL);
	sign = timeval_subtract (&diff, begin, &end);
	p->result = R_ABS (((double)(diff.tv_sec)
		+ ((double)diff.tv_usec / 1000000.)));
	return R_ABS (sign);
}