Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bm_float benchmark from pyperformance #43

Merged
merged 2 commits into from
Jan 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions benchmarks/bm_float.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
"""
Artificial, floating point-heavy benchmark originally used by Factor.

Adapted to mypyc by Jukka Lehtosalo
"""
from __future__ import annotations
from math import sin, cos, sqrt, isclose

from benchmarking import benchmark


POINTS = 100000


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

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

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

def normalize(self) -> None:
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: Point) -> Point:
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: list[Point]) -> Point:
next = points[0]
for p in points[1:]:
next = next.maximize(p)
return next


@benchmark()
def bm_float() -> None:
n = POINTS
points = []
for i in range(n):
points.append(Point(i))
for p in points:
p.normalize()
result = maximize(points)
assert isclose(result.x, 0.8944271890997864)
assert isclose(result.y, 1.0)
assert isclose(result.z, 0.4472135954456972)