8 * This returns the frequency of the TBR in cycles per second.
10 static double GetTBRFreq(void) {
11 mach_timebase_info_data_t tinfo
;
12 mach_timebase_info(&tinfo
);
14 double machRatio
= (double)tinfo
.numer
/ (double)tinfo
.denom
;
19 * Return TBR Frequency, getting it lazily once. May not be thread safe.
21 static double TbrFreqLocal
= 0.0; // ration for NANOSECONDS
22 static double tbrFreq()
24 if(TbrFreqLocal
== 0.0) {
25 TbrFreqLocal
= GetTBRFreq();
26 printf("machRatio %e\n", TbrFreqLocal
);
32 double CPUTimeDeltaSec(CPUTime from
, CPUTime to
)
34 CPUTime delta
= to
- from
;
35 return (double)delta
* (tbrFreq() * (double)1e-9);
39 double CPUTimeDeltaMs(CPUTime from
, CPUTime to
)
41 CPUTime delta
= to
- from
;
42 return (double)delta
* (tbrFreq() * (double)1e-6);
46 double CPUTimeDeltaUs(CPUTime from
, CPUTime to
)
48 CPUTime delta
= to
- from
;
49 return (double)delta
* (tbrFreq() * (double)1e-3);
53 * Calculate the average of an array of doubles. The lowest and highest values
54 * are discarded if there are more than two samples. Typically used to get an
55 * average of a set of values returned from CPUTimeDelta*().
62 double lowest
= array
[0];
63 double highest
= array
[0];
65 for(unsigned dex
=0; dex
<arraySize
; dex
++) {
66 double curr
= array
[dex
];
80 return sum
/ (double)arraySize
;