summaryrefslogtreecommitdiffstatshomepage
path: root/tests/perf_bench/bm_float.py
blob: 9e55deaee53cfde988f83667ac771876c0e34630 (plain) (blame)
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
# Source: https://github.com/python/pyperformance
# License: MIT

# Artificial, floating point-heavy benchmark originally used by Factor.

from math import sin, cos, sqrt


class Point(object):
    __slots__ = ("x", "y", "z")

    def __init__(self, i):
        self.x = x = sin(i)
        self.y = cos(i) * 3
        self.z = (x * x) / 2

    def __repr__(self):
        return "<Point: x=%s, y=%s, z=%s>" % (self.x, self.y, self.z)

    def normalize(self):
        x = self.x
        y = self.y
        z = self.z
        norm = sqrt(x * x + y * y + z * z)
        self.x /= norm
        self.y /= norm
        self.z /= norm

    def maximize(self, other):
        self.x = self.x if self.x > other.x else other.x
        self.y = self.y if self.y > other.y else other.y
        self.z = self.z if self.z > other.z else other.z
        return self


def maximize(points):
    next = points[0]
    for p in points[1:]:
        next = next.maximize(p)
    return next


def benchmark(n):
    points = [None] * n
    for i in range(n):
        points[i] = Point(i)
    for p in points:
        p.normalize()
    return maximize(points)


###########################################################################
# Benchmark interface

bm_params = {
    (50, 25): (1, 150),
    (100, 100): (1, 250),
    (1000, 1000): (10, 1500),
    (5000, 1000): (20, 3000),
}


def bm_setup(params):
    state = None

    def run():
        nonlocal state
        for _ in range(params[0]):
            state = benchmark(params[1])

    def result():
        return params[0] * params[1], "Point(%.4f, %.4f, %.4f)" % (state.x, state.y, state.z)

    return run, result