Run a benchmark and return timing statistics Args: func: Function to benchmark *args: Arguments to pass to func warmup: Number of warmup iterations iterations: Number of measurement iterations repeat: Number of times to repeat each measurement num
(func, *args, warmup=3, iterations=20, repeat=5, number=10000)
| 471 | |
| 472 | |
| 473 | def run_benchmark(func, *args, warmup=3, iterations=20, repeat=5, number=10000): |
| 474 | """Run a benchmark and return timing statistics |
| 475 | |
| 476 | Args: |
| 477 | func: Function to benchmark |
| 478 | *args: Arguments to pass to func |
| 479 | warmup: Number of warmup iterations |
| 480 | iterations: Number of measurement iterations |
| 481 | repeat: Number of times to repeat each measurement |
| 482 | number: Number of times to call func per measurement (inner loop) |
| 483 | |
| 484 | Returns: |
| 485 | (mean_time_per_call, stdev_time_per_call) |
| 486 | """ |
| 487 | # Warmup |
| 488 | for _ in range(warmup): |
| 489 | for _ in range(number): |
| 490 | func(*args) |
| 491 | |
| 492 | # Benchmark - run func 'number' times per measurement |
| 493 | times = [] |
| 494 | for _ in range(iterations): |
| 495 | timer = timeit.Timer(lambda: func(*args)) |
| 496 | iteration_times = timer.repeat(repeat=repeat, number=number) |
| 497 | # Convert total time to time per call |
| 498 | times.extend([t / number for t in iteration_times]) |
| 499 | |
| 500 | mean = statistics.mean(times) |
| 501 | stdev = statistics.stdev(times) if len(times) > 1 else 0 |
| 502 | return mean, stdev |
| 503 | |
| 504 | |
| 505 | def format_time(seconds): |
no test coverage detected