| 110 | } |
| 111 | |
| 112 | void Benchmark(const Options& options, const BenchmarkFn& fn, Stats* stats) { |
| 113 | // If neither max_seconds or max_iters is set, stop at kDefaultMicros. |
| 114 | const int64 max_us = (options.max_micros <= 0 && options.max_iters <= 0) |
| 115 | ? Options::kDefaultMicros |
| 116 | : options.max_micros; |
| 117 | printf("Running benchmark for %lld us\n", max_us); |
| 118 | const int64 start_us = NowMicros(); |
| 119 | int64 iters = 0; |
| 120 | while (true) { |
| 121 | const int64 iter_start_us = NowMicros(); |
| 122 | fn(); |
| 123 | const int64 end_us = NowMicros(); |
| 124 | // Collect stats and decide whether to stop. |
| 125 | stats->per_iter_us.push_back(end_us - iter_start_us); |
| 126 | const int64 total_us = end_us - start_us; |
| 127 | ++iters; |
| 128 | if ((max_us > 0 && total_us >= max_us) || |
| 129 | (options.max_iters > 0 && iters >= options.max_iters)) { |
| 130 | stats->total_us = total_us; |
| 131 | break; |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | } // namespace benchmark |
| 137 | } // namespace tfcompile |