<commit_before>"""
Benchmarks for argument dispatching and call overhead of ``@jit`` functions.
"""

import numpy as np

from numba import jit


rec_dtype = np.dtype([('a', np.float64),
                      ('b', np.int32),
                      ('c', np.complex64),
                      ])

samples = {
    'bool': True,
    'int': 100000,
    'float': 0.5,
    'complex': 0.5 + 1.0j,
    'array_1d': np.zeros(10, dtype=np.int64),
    'array_3d': np.zeros(20, dtype=np.float64).reshape(2, 2, 5),
    'array_records': np.zeros(10, dtype=rec_dtype),
    'recarray': np.recarray(10, dtype=rec_dtype),
    'tuple': (0.5, 1.0j, ()),
    'record': np.empty(1, dtype=rec_dtype)[0],
    'bytearray': bytearray(3),
    }

@jit(nopython=True)
def binary(x, y):
    pass

@jit(forceobj=True)
def binary_pyobj(x, y):
    pass


def setup():
    """
    Precompile jitted functions.
    """
    for tp in samples.values():
        binary(tp, tp)
    binary_pyobj(object(), object())


class NoPythonDispatch:

    # We repeat 1000 times so as to make the overhead of benchmark launching
    # negligible.

    @classmethod
    def generate_benchmarks(cls, names):
        for name in names:
            def timefunc(self, arg=samples[name]):
                for i in range(1000):
                    binary(arg, arg)
            timefunc.__name__ = "time_dispatch_" + name
            setattr(cls, timefunc.__name__, timefunc)


NoPythonDispatch.generate_benchmarks(samples.keys())


class PyObjectDispatch:

    def time_dispatch_pyobject(self):
        x = object()
        for i in range(1000):
            binary_pyobj(x, x)<commit_msg>Document the benchmark a bit<commit_after>"""
Benchmarks for argument dispatching and call overhead of ``@jit`` functions.
"""

import numpy as np

from numba import jit


rec_dtype = np.dtype([('a', np.float64),
                      ('b', np.int32),
                      ('c', np.complex64),
                      ])


samples = {
    'bool': True,
    'int': 100000,
    'float': 0.5,
    'complex': 0.5 + 1.0j,
    'array_1d': np.zeros(10, dtype=np.int64),
    'array_3d': np.zeros(20, dtype=np.float64).reshape(2, 2, 5),
    'array_records': np.zeros(10, dtype=rec_dtype),
    'recarray': np.recarray(10, dtype=rec_dtype),
    'tuple': (0.5, 1.0j, ()),
    'record': np.empty(1, dtype=rec_dtype)[0],
    'bytearray': bytearray(3),
    }

@jit(nopython=True)
def binary(x, y):
    pass

@jit(forceobj=True)
def binary_pyobj(x, y):
    pass


def setup():
    """
    Precompile jitted functions.  This will register many specializations
    to choose from.
    """
    for tp in samples.values():
        binary(tp, tp)
    binary_pyobj(object(), object())


class NoPythonDispatch:
    """
    Time dispatching to a jitted function's specializations based on argument
    types.
    This stresses two things:
    - the typing of arguments (from argument value to typecode)
    - the selection of the best specialization amongst all the known ones
    """

    # We repeat 1000 times so as to make the overhead of benchmark launching
    # negligible.

    @classmethod
    def generate_benchmarks(cls, names):
        for name in names:
            def timefunc(self, arg=samples[name]):
                func = binary
                for i in range(1000):
                    func(arg, arg)
            timefunc.__name__ = "time_dispatch_" + name
            setattr(cls, timefunc.__name__, timefunc)


NoPythonDispatch.generate_benchmarks(samples.keys())


class PyObjectDispatch:

    def time_dispatch_pyobject(self):
        x = object()
        for i in range(1000):
            binary_pyobj(x, x)
{'fraction_non_alphanumeric': 0.13001649257833975, 'fraction_numerical': 0.029686641011544807, 'mean_word_length': 2.4822966507177036, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6173050', 'n_tokens_mistral': 1265, 'n_tokens_neox': 1171, 'n_words': 314}