2 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * VMWARE BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 * Common perf code. This should be re-usable with other APIs.
34 #define snprintf _snprintf
38 /* Need to add a fflush windows console with mingw, otherwise nothing
39 * shows up until program exit. May want to add logging here.
42 perf_printf(const char *format
, ...)
48 vfprintf(stdout
, format
, ap
);
57 * Run function 'f' for enough iterations to reach a steady state.
58 * Return the rate (iterations/second).
61 PerfMeasureRate(PerfRateFunc f
)
63 const double minDuration
= 1.0;
64 double rate
= 0.0, prevRate
= 0.0;
67 /* Compute initial number of iterations to try.
68 * If the test function is pretty slow this helps to avoid
69 * extraordarily long run times.
73 const double t0
= PerfGetTime();
76 f(subiters
); /* call the rendering function */
79 } while (t1
- t0
< 0.1 * minDuration
);
81 /*perf_printf("initial subIters = %u\n", subiters);*/
84 const double t0
= PerfGetTime();
89 f(subiters
); /* call the rendering function */
92 } while (t1
- t0
< minDuration
);
94 rate
= iters
/ (t1
- t0
);
97 perf_printf("prevRate %f rate %f ratio %f iters %u\n",
98 prevRate
, rate
, rate
/prevRate
, iters
);
100 /* Try and speed the search up by skipping a few steps:
102 if (rate
> prevRate
* 1.6)
104 else if (rate
> prevRate
* 1.2)
106 else if (rate
> prevRate
* 1.05)
115 perf_printf("%s returning iters %u rate %f\n", __FUNCTION__
, subiters
, rate
);
120 /* Note static buffer, can only use once per printf.
123 PerfHumanFloat( double d
)
127 if (d
> 1000000000.0)
128 snprintf(buf
, sizeof(buf
), "%.1f billion", d
/ 1000000000.0);
129 else if (d
> 1000000.0)
130 snprintf(buf
, sizeof(buf
), "%.1f million", d
/ 1000000.0);
132 snprintf(buf
, sizeof(buf
), "%.1f thousand", d
/ 1000.0);
134 snprintf(buf
, sizeof(buf
), "%.1f", d
);