File: test_benchmark.py

package info (click to toggle)
python-multipledispatch 1.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 272 kB
  • sloc: python: 1,116; makefile: 141
file content (54 lines) | stat: -rw-r--r-- 1,083 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from multipledispatch import dispatch
import pytest


@dispatch(int)
def isint(x):
    return True


@dispatch(object)
def isint(x):
    return False


@dispatch(object, object)
def isint(x, y):
    return False


@pytest.mark.parametrize("val", [1, "a"])
def test_benchmark_call_single_dispatch(benchmark, val):
    benchmark(isint, val)


@pytest.mark.parametrize("val", [(1, 4)])
def test_benchmark_call_multiple_dispatch(benchmark, val):
    benchmark(isint, *val)


def test_benchmark_add_and_use_instance(benchmark):
    namespace = {}

    @benchmark
    def inner():
        @dispatch(int, int, namespace=namespace)
        def mul(x, y):
            return x * y

        @dispatch(str, int, namespace=namespace)
        def mul(x, y):
            return x * y

        @dispatch(int, int, [float], namespace=namespace)
        def mul(x, y, *args):
            return x * y

        @dispatch([int], namespace=namespace)
        def mul(*args):
            return sum(args)

        mul(4, 5)
        mul("x", 5)
        mul(1, 2, 3.0, 4.0, 5.0)
        mul(1, 2, 3, 4, 5)