Skip to content

Decorator

Benchmark decorator utilities for easier function benchmarking.

This module provides decorators for benchmarking functions with configurable parameters and settings. It allows for easy setup and running of benchmarks.

BenchDecorator

Decorator for benchmarking functions.

Source code in easybench/decorator.py
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
class BenchDecorator:
    """Decorator for benchmarking functions."""

    def __init__(self) -> None:
        """Initialize BenchDecorator with storage for deferred functions."""
        self._deferred_functions: dict[str, list[BenchmarkableFunction]] = {}

    def _initialize_bench_attributes(self, func: Callable) -> BenchmarkableFunction:
        """
        Initialize benchmark attributes on a function if they don't exist.

        Args:
            func: The function to initialize benchmark attributes for

        Returns:
            The function cast as BenchmarkableFunction with all necessary attributes

        """
        func = cast("BenchmarkableFunction", func)
        if not hasattr(func, "bench"):
            func.bench = FunctionBench(func)
            func.fixtures = {}
            # Initialize last_results together with bench
            func.last_results = {}
            # Initialize defer attribute
            func.defer = False
            # Initialize params_list attribute
            func.params_list = None
            # Add the function to its global namespace to enable recursion
            self._register_in_globals(func)

        return func

    def _register_in_globals(self, func: BenchmarkableFunction) -> None:
        """
        Register the function in its global namespace to enable recursion.

        Args:
            func: The function to register in the global namespace

        """
        if hasattr(func, "__globals__") and hasattr(func, "__name__"):
            # Register the function in the module's global namespace
            # with its original name
            func.__globals__[func.__name__] = func

            # Add self-reference to the function's closure if needed
            code = getattr(func, "__code__", None)
            closure = getattr(func, "__closure__", None)
            freevars = getattr(code, "co_freevars", ()) if code else ()
            if closure is not None and func.__name__ in freevars:
                for i, var_name in enumerate(freevars):
                    if var_name == func.__name__ and isinstance(closure[i], CellType):
                        object.__setattr__(closure[i], "cell_contents", func)
                        break

    def __call__(
        self,
        *args: object,
        **kwargs: object,
    ) -> Callable:
        """
        EasyBench benchmark decorator.

        Example:
            ```python
            @bench(
                item=123,
                big_list=lambda: list(range(1_000_000)),
            )
            def add_item(item, big_list):
                big_list.append(item)
            ```

            # Using a list of BenchParams for comparison
            ```python
            params1 = BenchParams(
                name="Small", params={"lst": lambda: list(range(1000))},
            )
            params2 = BenchParams(
                name="Large", params={"lst": lambda: list(range(10000))},
            )

            @bench([params1, params2])
            def pop_first(lst):
                return lst.pop(0)
            ```

        Returns:
            A decorated function with benchmark capabilities

        """
        # Handle list of BenchParams as first argument
        if (
            len(args) == 1
            and isinstance(args[0], list)
            and not kwargs
            and all(isinstance(param, BenchParams) for param in args[0])
        ):
            return self._decorate_with_bench_param_list(args[0])

        # Handle BenchParams instance as first argument
        if len(args) == 1 and isinstance(args[0], BenchParams) and not kwargs:
            return self._decorate_with_bench_param(args[0])

        # Handle direct function decoration: @bench
        if len(args) == 1 and callable(args[0]) and not kwargs:
            return self._decorate(args[0])

        # Handle parameterized decoration: @bench(param=value)
        def decorator(func: Callable) -> Callable:
            return self._decorate(func, **kwargs)

        return decorator

    def _process_single_param_set(
        self,
        func: Callable,
        params: BenchParams,
        index: int,
        func_name: str,
    ) -> tuple[str, ResultType | None, BenchConfig | None]:
        """
        Process a single parameter set for benchmarking.

        Args:
            func: The original function to benchmark
            params: The parameter set to use
            index: The index of this parameter in the list
            func_name: Name of the function

        Returns:
            A tuple of (param_name, results_dict, config)

        """
        # Determine parameter name
        param_name = f"params_{index+1}"
        if params.name:
            param_name = params.name

        # Decorate with this parameter set (with empty reporters)
        decorated_func = self._decorate_with_bench_param(params, reporters=[])(func)

        # Extract results from the function
        bench_func = cast("BenchmarkableFunction", decorated_func)
        results_dict = None
        config = None

        if bench_func.last_results:
            results_dict = bench_func.last_results[func_name]
            config = bench_func.bench.bench_config.model_copy(deep=True)

        return param_name, results_dict, config

    def _display_comparison_results(
        self,
        all_results: ResultsType,
        first_config: BenchConfig,
        original_reporters: list[Reporter] | None,
    ) -> None:
        """
        Display comparison results.

        Args:
            all_results: Dictionary of all benchmark results
            first_config: The first benchmark configuration
            original_reporters: Original reporters from the function

        """
        comparison_config = first_config
        if original_reporters is not None:
            comparison_config.reporters = original_reporters
        elif comparison_config.reporters == []:
            comparison_config.reporters = [ConsoleReporter()]

        comparison = EasyBench(bench_config=comparison_config)
        comparison.report_results(
            results=all_results,
            config=comparison_config,
        )

    def _decorate_with_bench_param_list(
        self,
        params_list: list[BenchParams],
    ) -> Callable:
        """
        Set up the function for benchmarking using a list of BenchParams instances.

        This allows comparing different parameter configurations for the same function.

        Args:
            params_list: List of BenchParams instances with benchmark configurations

        Returns:
            A decorator function that configures the function with multiple
            BenchParams and compares results

        """

        def decorator(func: Callable) -> Callable:
            # Initialize benchmark attributes
            func = self._initialize_bench_attributes(func)

            # Store the params_list with the function for possible deferred execution
            func.params_list = params_list

            # If deferred, register in the group and return
            if func.defer:
                return func

            # Not deferred - run benchmarks immediately
            # Store original function
            orig_func = func
            func_name = func.__name__

            # Store original reporters from the function if available
            original_reporters = None
            if hasattr(func, "bench") and hasattr(func.bench, "bench_config"):
                bench_func = cast("BenchmarkableFunction", func)
                original_reporters = bench_func.bench.bench_config.reporters

            # Store results for each parameter set
            all_results: ResultsType = {}
            first_config = None

            # Run benchmarks for each parameter set
            for i, params in enumerate(params_list):
                param_name, results, config = self._process_single_param_set(
                    func,
                    params,
                    i,
                    func_name,
                )

                if results:
                    all_results[f"{func_name} ({param_name})"] = results
                    if first_config is None:
                        first_config = config

            # Display results if we have results
            if len(all_results) >= 1 and first_config:
                self._display_comparison_results(
                    all_results,
                    first_config,
                    original_reporters,
                )

            # Return the original function
            return orig_func

        return decorator

    def _decorate_with_bench_param(
        self,
        param: BenchParams,
        reporters: list[Reporter] | None = None,
    ) -> Callable:
        """
        Set up the function for benchmarking using a BenchParams instance.

        Args:
            param: BenchParams instance with benchmark configuration
            reporters: List of reporters for benchmark

        Returns:
            A decorator function that configures the function with the BenchParams

        """

        def decorator(func: Callable) -> Callable:
            # Initialize benchmark attributes
            func = self._initialize_bench_attributes(func)

            if reporters is not None:
                func.bench.bench_config.reporters = reporters

            # Apply function parameters
            if param.fn_params:
                for name, value in param.fn_params.items():
                    func.fixtures[name] = lambda v=value: v

            # Apply bench parameters
            if param.params:
                for name, value in param.params.items():
                    if callable(value) and not isinstance(value, type):
                        # For callables like lambdas, register the callable itself
                        func.fixtures[name] = value
                    else:
                        # For values, create a lambda that returns the value
                        func.fixtures[name] = lambda v=value: v

            # Run benchmark if all parameters are satisfied
            self._maybe_run_benchmark(func)

            return func

        return decorator

    def _decorate(self, func: Callable, **kwargs: object) -> Callable:
        """
        Set up the function for benchmarking.

        Args:
            func: The function to benchmark
            **kwargs: Benchmark parameters

        Returns:
            The decorated function

        """
        # Initialize benchmark attributes
        func = self._initialize_bench_attributes(func)

        # Register variables as fixtures
        for name, value in kwargs.items():
            if callable(value) and not isinstance(value, type):
                # For callables like lambdas, register the callable itself
                func.fixtures[name] = value
            else:
                # For values, create a lambda that returns the value
                func.fixtures[name] = lambda v=value: v

        # Check if all required parameters are available and run if they are
        self._maybe_run_benchmark(func)
        return func

    def _reset_bench(
        self,
        func: BenchmarkableFunction,
        *,
        reset_params: bool = True,
        reset_config: bool = True,
    ) -> None:
        """
        Reset benchmark settings.

        Args:
            func: The benchmarked function
            reset_params: Whether to reset parameters
            reset_config: Whether to reset configuration

        """
        if reset_params:
            func.fixtures = {}

        if reset_config:
            func.bench.bench_config = BenchConfig()

    def fn_params(self, **kwargs: object) -> Callable:
        """
        Configure benchmark function parameters.

        Example:
            ```python
            @bench.fn_params(op=lambda x: x + 1)
            def apply_operation(op):
                return op(12345)
            ```

        Returns:
            A decorator function that configures benchmark parameters

        """

        def decorator(func: Callable) -> Callable:
            # Initialize benchmark attributes
            func = self._initialize_bench_attributes(func)

            # Register function parameters as fixtures
            for name, value in kwargs.items():
                func.fixtures[name] = lambda v=value: v

            self._maybe_run_benchmark(func)
            return func

        return decorator

    def _maybe_run_benchmark(self, func: BenchmarkableFunction) -> None:
        """Run the benchmark if all parameters are available and not deferred."""
        # Skip if deferred
        if func.defer:
            return

        # Get function signature to determine required parameters
        sig = inspect.signature(func)

        # Get parameters that have no defaults and must be provided
        required_params = {
            name
            for name, param in sig.parameters.items()
            if param.default is inspect.Parameter.empty
        }

        # Check if all required parameters are available in fixtures
        provided_params = set(func.fixtures.keys())

        if required_params.issubset(provided_params):
            # All parameters are satisfied, run the benchmark
            fixture_registry: FixtureRegistry = {
                "trial": func.fixtures,
                "function": {},
                "class": {},
            }
            results = func.bench.bench(fixture_registry=fixture_registry)
            func.last_results = results

            # Reset bench
            self._reset_bench(func, reset_config=False)

    @overload
    def config(self, /) -> Callable: ...

    @overload
    def config(self, config: BenchConfig, /, **kwargs: object) -> Callable: ...

    @overload
    def config(
        self,
        /,
        *,
        trials: int | None = None,
        loops_per_trial: int | None = None,
        warmups: int | None = None,
        sort_by: SortType | None = None,
        reverse: bool | None = None,
        memory: bool | MemoryUnit | str | None = None,
        time: bool | TimeUnit | str | None = None,
        color: bool | None = None,
        show_output: bool | None = None,
        reporters: list[Reporter] | None = None,
        progress: bool | Callable | None = None,
        clip_outliers: float | None = None,
        defer: bool | str | None = None,
    ) -> Callable: ...

    def config(self, config: BenchConfig | None = None, /, **kwargs: Any) -> Callable:
        """
        Configure benchmark settings.

        Example:
            ```python
            # Using keyword arguments
            @bench(
                item=123,
                big_list=lambda: list(range(1_000_000)),
            )
            @bench.config(trials=5, loops_per_trial=10, memory=True)
            def add_item(item, big_list):
                big_list.append(item)

            # Using BenchConfig as positional argument
            my_config = BenchConfig(trials=10, memory=True)
            @bench.config(my_config)
            def another_function():
                pass
            ```

        Args:
            config: Optional BenchConfig instance as positional argument
            **kwargs: Configuration parameters to set

        Returns:
            A decorator function that configures benchmark settings

        """

        def decorator(func: Callable) -> Callable:
            # Initialize benchmark attributes
            func = self._initialize_bench_attributes(func)

            # Extract defer parameter (not part of BenchConfig)
            defer = kwargs.pop("defer", False)
            func.defer = defer

            # Register function in group if a group name is provided
            if isinstance(defer, str):
                if defer not in self._deferred_functions:
                    self._deferred_functions[defer] = []
                self._deferred_functions[defer].append(func)

            # Create partial config from kwargs
            partial_config = PartialBenchConfig(**kwargs)
            # Update benchmark config with merged values
            func.bench.bench_config = partial_config.merge_with(
                config or func.bench.bench_config,
            )

            # Check if all required parameters are available
            self._maybe_run_benchmark(func)
            return func

        return decorator

    def grid(
        self,
        params_lists: Iterable[Iterable[BenchParams]],
    ) -> Callable:
        """
        Create a decorator that applies a Cartesian product of parameter lists.

        This creates all combinations of the parameter sets for benchmarking.

        Example:
            ```python
            sizes = [
                BenchParams(name="Small", params={"size": 100}),
                BenchParams(name="Large", params={"size": 10000}),
            ]
            ops = [
                BenchParams(name="Append", fn_params={"op": lambda x: x.append(0)}),
                BenchParams(name="Pop", fn_params={"op": lambda x: x.pop()}),
            ]

            @bench.grid([sizes, ops])
            def operation(size, op):
                # Will run with all combinations:
                # (Small, Append), (Small, Pop), (Large, Append), (Large, Pop)
                lst = list(range(size))
                op(lst)
            ```

        Args:
            params_lists: An iterable of iterables of BenchParams to combine

        Returns:
            A decorator function that applies all parameter combinations

        """

        def decorator(func: Callable) -> Callable:
            # Convert all parameter lists to actual lists
            converted_lists = []
            for params in params_lists:
                if isinstance(params, (list, tuple)):
                    converted_lists.append(list(params))
                else:
                    # Handle iterables by converting to list
                    converted_lists.append(list(params))

            # Handle the case with no parameter lists
            if not converted_lists:
                return func

            # Handle the case with just one parameter list
            if len(converted_lists) == 1:
                return self._decorate_with_bench_param_list(converted_lists[0])(func)

            # Start with the first list
            result_params = converted_lists[0]

            # Multiply with each subsequent list to get Cartesian product
            for params_list in converted_lists[1:]:
                new_result = []
                for param1 in result_params:
                    for param2 in params_list:
                        # Use the multiplication operator defined in BenchParams
                        combined_param = param1 * param2
                        new_result.append(combined_param)
                result_params = new_result

            # Apply the combined parameters using _decorate_with_bench_param_list
            return self._decorate_with_bench_param_list(result_params)(func)

        return decorator

    def run(
        self,
        group: str,
        config: BenchConfig | None = None,
    ) -> ResultsType:
        """
        Run all benchmarks in a specified group.

        Args:
            group: The group name to run
            config: Optional BenchConfig to use for all benchmarks in the group

        Returns:
            Combined results from all benchmarks in the group

        Example:
            ```python
            @bench.config(defer="examples")
            def example1(x):
                return x * 2

            @bench.config(defer="examples")
            def example2(x):
                return x * 3

            # Run all examples
            bench.run("examples")
            ```

        """
        if group not in self._deferred_functions:
            msg = f"No functions in group '{group}'"
            raise ValueError(msg)

        # Get all functions in the group
        functions = self._deferred_functions[group]

        if not functions:
            msg = f"Group '{group}' exists but contains no functions"
            raise ValueError(msg)

        # Use the config of the last added function if no config provided
        final_config = config or functions[-1].bench.bench_config.model_copy(deep=True)

        all_results: ResultsType = {}

        # Process each function
        for func in functions:
            # Prepare config
            _config = final_config.model_copy(deep=True)
            _config.reporters = []

            # Use individual config for loops_per_trial
            _config.loops_per_trial = func.bench.bench_config.loops_per_trial

            # Check if this function has params_list for comparison benchmarking
            if func.params_list:
                # Handle parameter comparison benchmarking
                func_name = func.__name__

                # Run benchmarks for each parameter set
                org_defer = func.defer
                org_config = func.bench.bench_config
                func.defer = False
                func.bench.bench_config = _config
                for i, params in enumerate(func.params_list):
                    param_name, results, _ = self._process_single_param_set(
                        func,
                        params,
                        i,
                        func_name,
                    )

                    if results:
                        all_results[f"{func_name} ({param_name})"] = results
                func.defer = org_defer
                func.bench.bench_config = org_config
            else:
                # Standard benchmark for a single function
                fixture_registry: FixtureRegistry = {
                    "trial": func.fixtures,
                    "function": {},
                    "class": {},
                }
                _results = func.bench.bench(
                    config=_config,
                    fixture_registry=fixture_registry,
                )

                # Add results to the combined results
                if _results:
                    all_results.update(_results)

        # Display combined results using the final config
        if all_results:
            comparison = EasyBench(bench_config=final_config)
            comparison.report_results(
                results=all_results,
                config=final_config,
            )

        return all_results

__call__(*args, **kwargs)

EasyBench benchmark decorator.

Example
@bench(
    item=123,
    big_list=lambda: list(range(1_000_000)),
)
def add_item(item, big_list):
    big_list.append(item)

Using a list of BenchParams for comparison

params1 = BenchParams(
    name="Small", params={"lst": lambda: list(range(1000))},
)
params2 = BenchParams(
    name="Large", params={"lst": lambda: list(range(10000))},
)

@bench([params1, params2])
def pop_first(lst):
    return lst.pop(0)

Returns:

Type Description
Callable

A decorated function with benchmark capabilities

Source code in easybench/decorator.py
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
def __call__(
    self,
    *args: object,
    **kwargs: object,
) -> Callable:
    """
    EasyBench benchmark decorator.

    Example:
        ```python
        @bench(
            item=123,
            big_list=lambda: list(range(1_000_000)),
        )
        def add_item(item, big_list):
            big_list.append(item)
        ```

        # Using a list of BenchParams for comparison
        ```python
        params1 = BenchParams(
            name="Small", params={"lst": lambda: list(range(1000))},
        )
        params2 = BenchParams(
            name="Large", params={"lst": lambda: list(range(10000))},
        )

        @bench([params1, params2])
        def pop_first(lst):
            return lst.pop(0)
        ```

    Returns:
        A decorated function with benchmark capabilities

    """
    # Handle list of BenchParams as first argument
    if (
        len(args) == 1
        and isinstance(args[0], list)
        and not kwargs
        and all(isinstance(param, BenchParams) for param in args[0])
    ):
        return self._decorate_with_bench_param_list(args[0])

    # Handle BenchParams instance as first argument
    if len(args) == 1 and isinstance(args[0], BenchParams) and not kwargs:
        return self._decorate_with_bench_param(args[0])

    # Handle direct function decoration: @bench
    if len(args) == 1 and callable(args[0]) and not kwargs:
        return self._decorate(args[0])

    # Handle parameterized decoration: @bench(param=value)
    def decorator(func: Callable) -> Callable:
        return self._decorate(func, **kwargs)

    return decorator

__init__()

Initialize BenchDecorator with storage for deferred functions.

Source code in easybench/decorator.py
52
53
54
def __init__(self) -> None:
    """Initialize BenchDecorator with storage for deferred functions."""
    self._deferred_functions: dict[str, list[BenchmarkableFunction]] = {}

config(config=None, /, **kwargs)

config() -> Callable
config(
    config: BenchConfig, /, **kwargs: object
) -> Callable
config(
    *,
    trials: int | None = None,
    loops_per_trial: int | None = None,
    warmups: int | None = None,
    sort_by: SortType | None = None,
    reverse: bool | None = None,
    memory: bool | MemoryUnit | str | None = None,
    time: bool | TimeUnit | str | None = None,
    color: bool | None = None,
    show_output: bool | None = None,
    reporters: list[Reporter] | None = None,
    progress: bool | Callable | None = None,
    clip_outliers: float | None = None,
    defer: bool | str | None = None
) -> Callable

Configure benchmark settings.

Example
# Using keyword arguments
@bench(
    item=123,
    big_list=lambda: list(range(1_000_000)),
)
@bench.config(trials=5, loops_per_trial=10, memory=True)
def add_item(item, big_list):
    big_list.append(item)

# Using BenchConfig as positional argument
my_config = BenchConfig(trials=10, memory=True)
@bench.config(my_config)
def another_function():
    pass

Parameters:

Name Type Description Default
config BenchConfig | None

Optional BenchConfig instance as positional argument

None
**kwargs Any

Configuration parameters to set

{}

Returns:

Type Description
Callable

A decorator function that configures benchmark settings

Source code in easybench/decorator.py
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
def config(self, config: BenchConfig | None = None, /, **kwargs: Any) -> Callable:
    """
    Configure benchmark settings.

    Example:
        ```python
        # Using keyword arguments
        @bench(
            item=123,
            big_list=lambda: list(range(1_000_000)),
        )
        @bench.config(trials=5, loops_per_trial=10, memory=True)
        def add_item(item, big_list):
            big_list.append(item)

        # Using BenchConfig as positional argument
        my_config = BenchConfig(trials=10, memory=True)
        @bench.config(my_config)
        def another_function():
            pass
        ```

    Args:
        config: Optional BenchConfig instance as positional argument
        **kwargs: Configuration parameters to set

    Returns:
        A decorator function that configures benchmark settings

    """

    def decorator(func: Callable) -> Callable:
        # Initialize benchmark attributes
        func = self._initialize_bench_attributes(func)

        # Extract defer parameter (not part of BenchConfig)
        defer = kwargs.pop("defer", False)
        func.defer = defer

        # Register function in group if a group name is provided
        if isinstance(defer, str):
            if defer not in self._deferred_functions:
                self._deferred_functions[defer] = []
            self._deferred_functions[defer].append(func)

        # Create partial config from kwargs
        partial_config = PartialBenchConfig(**kwargs)
        # Update benchmark config with merged values
        func.bench.bench_config = partial_config.merge_with(
            config or func.bench.bench_config,
        )

        # Check if all required parameters are available
        self._maybe_run_benchmark(func)
        return func

    return decorator

fn_params(**kwargs)

Configure benchmark function parameters.

Example
@bench.fn_params(op=lambda x: x + 1)
def apply_operation(op):
    return op(12345)

Returns:

Type Description
Callable

A decorator function that configures benchmark parameters

Source code in easybench/decorator.py
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
def fn_params(self, **kwargs: object) -> Callable:
    """
    Configure benchmark function parameters.

    Example:
        ```python
        @bench.fn_params(op=lambda x: x + 1)
        def apply_operation(op):
            return op(12345)
        ```

    Returns:
        A decorator function that configures benchmark parameters

    """

    def decorator(func: Callable) -> Callable:
        # Initialize benchmark attributes
        func = self._initialize_bench_attributes(func)

        # Register function parameters as fixtures
        for name, value in kwargs.items():
            func.fixtures[name] = lambda v=value: v

        self._maybe_run_benchmark(func)
        return func

    return decorator

grid(params_lists)

Create a decorator that applies a Cartesian product of parameter lists.

This creates all combinations of the parameter sets for benchmarking.

Example
sizes = [
    BenchParams(name="Small", params={"size": 100}),
    BenchParams(name="Large", params={"size": 10000}),
]
ops = [
    BenchParams(name="Append", fn_params={"op": lambda x: x.append(0)}),
    BenchParams(name="Pop", fn_params={"op": lambda x: x.pop()}),
]

@bench.grid([sizes, ops])
def operation(size, op):
    # Will run with all combinations:
    # (Small, Append), (Small, Pop), (Large, Append), (Large, Pop)
    lst = list(range(size))
    op(lst)

Parameters:

Name Type Description Default
params_lists Iterable[Iterable[BenchParams]]

An iterable of iterables of BenchParams to combine

required

Returns:

Type Description
Callable

A decorator function that applies all parameter combinations

Source code in easybench/decorator.py
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
def grid(
    self,
    params_lists: Iterable[Iterable[BenchParams]],
) -> Callable:
    """
    Create a decorator that applies a Cartesian product of parameter lists.

    This creates all combinations of the parameter sets for benchmarking.

    Example:
        ```python
        sizes = [
            BenchParams(name="Small", params={"size": 100}),
            BenchParams(name="Large", params={"size": 10000}),
        ]
        ops = [
            BenchParams(name="Append", fn_params={"op": lambda x: x.append(0)}),
            BenchParams(name="Pop", fn_params={"op": lambda x: x.pop()}),
        ]

        @bench.grid([sizes, ops])
        def operation(size, op):
            # Will run with all combinations:
            # (Small, Append), (Small, Pop), (Large, Append), (Large, Pop)
            lst = list(range(size))
            op(lst)
        ```

    Args:
        params_lists: An iterable of iterables of BenchParams to combine

    Returns:
        A decorator function that applies all parameter combinations

    """

    def decorator(func: Callable) -> Callable:
        # Convert all parameter lists to actual lists
        converted_lists = []
        for params in params_lists:
            if isinstance(params, (list, tuple)):
                converted_lists.append(list(params))
            else:
                # Handle iterables by converting to list
                converted_lists.append(list(params))

        # Handle the case with no parameter lists
        if not converted_lists:
            return func

        # Handle the case with just one parameter list
        if len(converted_lists) == 1:
            return self._decorate_with_bench_param_list(converted_lists[0])(func)

        # Start with the first list
        result_params = converted_lists[0]

        # Multiply with each subsequent list to get Cartesian product
        for params_list in converted_lists[1:]:
            new_result = []
            for param1 in result_params:
                for param2 in params_list:
                    # Use the multiplication operator defined in BenchParams
                    combined_param = param1 * param2
                    new_result.append(combined_param)
            result_params = new_result

        # Apply the combined parameters using _decorate_with_bench_param_list
        return self._decorate_with_bench_param_list(result_params)(func)

    return decorator

run(group, config=None)

Run all benchmarks in a specified group.

Parameters:

Name Type Description Default
group str

The group name to run

required
config BenchConfig | None

Optional BenchConfig to use for all benchmarks in the group

None

Returns:

Type Description
ResultsType

Combined results from all benchmarks in the group

Example
@bench.config(defer="examples")
def example1(x):
    return x * 2

@bench.config(defer="examples")
def example2(x):
    return x * 3

# Run all examples
bench.run("examples")
Source code in easybench/decorator.py
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
def run(
    self,
    group: str,
    config: BenchConfig | None = None,
) -> ResultsType:
    """
    Run all benchmarks in a specified group.

    Args:
        group: The group name to run
        config: Optional BenchConfig to use for all benchmarks in the group

    Returns:
        Combined results from all benchmarks in the group

    Example:
        ```python
        @bench.config(defer="examples")
        def example1(x):
            return x * 2

        @bench.config(defer="examples")
        def example2(x):
            return x * 3

        # Run all examples
        bench.run("examples")
        ```

    """
    if group not in self._deferred_functions:
        msg = f"No functions in group '{group}'"
        raise ValueError(msg)

    # Get all functions in the group
    functions = self._deferred_functions[group]

    if not functions:
        msg = f"Group '{group}' exists but contains no functions"
        raise ValueError(msg)

    # Use the config of the last added function if no config provided
    final_config = config or functions[-1].bench.bench_config.model_copy(deep=True)

    all_results: ResultsType = {}

    # Process each function
    for func in functions:
        # Prepare config
        _config = final_config.model_copy(deep=True)
        _config.reporters = []

        # Use individual config for loops_per_trial
        _config.loops_per_trial = func.bench.bench_config.loops_per_trial

        # Check if this function has params_list for comparison benchmarking
        if func.params_list:
            # Handle parameter comparison benchmarking
            func_name = func.__name__

            # Run benchmarks for each parameter set
            org_defer = func.defer
            org_config = func.bench.bench_config
            func.defer = False
            func.bench.bench_config = _config
            for i, params in enumerate(func.params_list):
                param_name, results, _ = self._process_single_param_set(
                    func,
                    params,
                    i,
                    func_name,
                )

                if results:
                    all_results[f"{func_name} ({param_name})"] = results
            func.defer = org_defer
            func.bench.bench_config = org_config
        else:
            # Standard benchmark for a single function
            fixture_registry: FixtureRegistry = {
                "trial": func.fixtures,
                "function": {},
                "class": {},
            }
            _results = func.bench.bench(
                config=_config,
                fixture_registry=fixture_registry,
            )

            # Add results to the combined results
            if _results:
                all_results.update(_results)

    # Display combined results using the final config
    if all_results:
        comparison = EasyBench(bench_config=final_config)
        comparison.report_results(
            results=all_results,
            config=final_config,
        )

    return all_results

BenchmarkableFunction

Bases: Protocol[P, R_co]

Function with bench.

Source code in easybench/decorator.py
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class BenchmarkableFunction(Protocol[P, R_co]):
    """Function with bench."""

    def __call__(self, *args: P.args, **kwds: P.kwargs) -> R_co:
        """Call method."""
        ...

    @property
    def __name__(self) -> str:
        """Name of the function."""
        ...

    bench: FunctionBench
    fixtures: dict[str, object]
    last_results: ResultsType
    defer: bool | str
    params_list: list[BenchParams] | None

__name__ property

Name of the function.

__call__(*args, **kwds)

Call method.

Source code in easybench/decorator.py
33
34
35
def __call__(self, *args: P.args, **kwds: P.kwargs) -> R_co:
    """Call method."""
    ...