| 497 | self.bias = saved_bias |
| 498 | |
| 499 | def _calibrate_inner(self, m, verbose): |
| 500 | get_time = self.get_time |
| 501 | |
| 502 | # Set up a test case to be run with and without profiling. Include |
| 503 | # lots of calls, because we're trying to quantify stopwatch overhead. |
| 504 | # Do not raise any exceptions, though, because we want to know |
| 505 | # exactly how many profile events are generated (one call event, + |
| 506 | # one return event, per Python-level call). |
| 507 | |
| 508 | def f1(n): |
| 509 | for i in range(n): |
| 510 | x = 1 |
| 511 | |
| 512 | def f(m, f1=f1): |
| 513 | for i in range(m): |
| 514 | f1(100) |
| 515 | |
| 516 | f(m) # warm up the cache |
| 517 | |
| 518 | # elapsed_noprofile <- time f(m) takes without profiling. |
| 519 | t0 = get_time() |
| 520 | f(m) |
| 521 | t1 = get_time() |
| 522 | elapsed_noprofile = t1 - t0 |
| 523 | if verbose: |
| 524 | print("elapsed time without profiling =", elapsed_noprofile) |
| 525 | |
| 526 | # elapsed_profile <- time f(m) takes with profiling. The difference |
| 527 | # is profiling overhead, only some of which the profiler subtracts |
| 528 | # out on its own. |
| 529 | p = Profile() |
| 530 | t0 = get_time() |
| 531 | p.runctx('f(m)', globals(), locals()) |
| 532 | t1 = get_time() |
| 533 | elapsed_profile = t1 - t0 |
| 534 | if verbose: |
| 535 | print("elapsed time with profiling =", elapsed_profile) |
| 536 | |
| 537 | # reported_time <- "CPU seconds" the profiler charged to f and f1. |
| 538 | total_calls = 0.0 |
| 539 | reported_time = 0.0 |
| 540 | for (filename, line, funcname), (cc, ns, tt, ct, callers) in \ |
| 541 | p.timings.items(): |
| 542 | if funcname in ("f", "f1"): |
| 543 | total_calls += cc |
| 544 | reported_time += tt |
| 545 | |
| 546 | if verbose: |
| 547 | print("'CPU seconds' profiler reported =", reported_time) |
| 548 | print("total # calls =", total_calls) |
| 549 | if total_calls != m + 1: |
| 550 | raise ValueError("internal error: total calls = %d" % total_calls) |
| 551 | |
| 552 | # reported_time - elapsed_noprofile = overhead the profiler wasn't |
| 553 | # able to measure. Divide by twice the number of calls (since there |
| 554 | # are two profiler events per call in this test) to get the hidden |
| 555 | # overhead per event. |
| 556 | mean = (reported_time - elapsed_noprofile) / 2.0 / total_calls |