summaryrefslogtreecommitdiff
path: root/benchmarks/benchmarks/bench_scalar.py
blob: 638f66df5bdeec0c059aed0c7204b9b2421333b2 (plain)
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
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
from .common import Benchmark, TYPES1

import numpy as np


class ScalarMath(Benchmark):
    # Test scalar math, note that each of these is run repeatedly to offset
    # the function call overhead to some degree.
    params = [TYPES1]
    param_names = ["type"]
    def setup(self, typename):
        self.num = np.dtype(typename).type(2)
        self.int32 = np.int32(2)
        self.int32arr = np.array(2, dtype=np.int32)

    def time_addition(self, typename):
        n = self.num
        res = n + n + n + n + n + n + n + n + n + n

    def time_addition_pyint(self, typename):
        n = self.num
        res = n + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1

    def time_multiplication(self, typename):
        n = self.num
        res = n * n * n * n * n * n * n * n * n * n

    def time_power_of_two(self, typename):
        n = self.num
        res = n**2, n**2, n**2, n**2, n**2, n**2, n**2, n**2, n**2, n**2

    def time_abs(self, typename):
        n = self.num
        res = abs(abs(abs(abs(abs(abs(abs(abs(abs(abs(n))))))))))

    def time_add_int32_other(self, typename):
        # Some mixed cases are fast, some are slow, this documents these
        # differences.  (When writing, it was fast if the type of the result
        # is one of the inputs.)
        int32 = self.int32
        other = self.num
        int32 + other
        int32 + other
        int32 + other
        int32 + other
        int32 + other

    def time_add_int32arr_and_other(self, typename):
        # `arr + scalar` hits the normal ufunc (array) paths.
        int32 = self.int32arr
        other = self.num
        int32 + other
        int32 + other
        int32 + other
        int32 + other
        int32 + other

    def time_add_other_and_int32arr(self, typename):
        # `scalar + arr` at some point hit scalar paths in some cases, and
        # these paths could be optimized more easily
        int32 = self.int32arr
        other = self.num
        other + int32
        other + int32
        other + int32
        other + int32
        other + int32


class ScalarStr(Benchmark):
    # Test scalar to str conversion
    params = [TYPES1]
    param_names = ["type"]

    def setup(self, typename):
        self.a = np.array([100] * 100, dtype=typename)

    def time_str_repr(self, typename):
        res = [str(x) for x in self.a]