Skip to content
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions benches/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,7 @@ rustflags = [
## Benchmark source

- <https://benchmarksgame-team.pages.debian.net/benchmarksgame/program/nbody-python3-2.html>
- The workloads marked as adapted from pyperformance come from
[python/pyperformance 1.14.0](https://github.com/python/pyperformance/tree/1.14.0/pyperformance/data-files/benchmarks).
Their `pyperf` runners and internal timers are removed so Criterion is the only measurement
harness, and some input sizes are reduced for CodSpeed simulation.
34 changes: 34 additions & 0 deletions benches/benchmarks/base64.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Adapted from python/pyperformance 1.14.0 (base64).
"""Exercise the base64 module's primary public APIs."""

import base64
import random


random_source = random.Random(12345)
data_tiny = random_source.randbytes(20)
data_small = random_source.randbytes(127)
data_medium = random_source.randbytes(3072)
data_large = random_source.randbytes(9000)
cases = (
(data_tiny, 45),
(data_small, 7),
(data_medium, 1),
(data_large, 1),
)


def exercise(encode, decode):
for data, count in cases:
encoded = encode(data)
for _ in range(count):
assert decode(encode(data)) == data
assert decode(encoded) == data


exercise(base64.b64encode, base64.b64decode)
exercise(base64.urlsafe_b64encode, base64.urlsafe_b64decode)
exercise(base64.b32encode, base64.b32decode)
exercise(base64.b16encode, base64.b16decode)
exercise(base64.a85encode, base64.a85decode)
exercise(base64.b85encode, base64.b85decode)
42 changes: 42 additions & 0 deletions benches/benchmarks/decimal_factorial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Adapted from python/pyperformance 1.14.0 (decimal_factorial).
"""
Calculate `factorial` using the decimal module.

- 2024-06-14: Michael Droettboom copied this from
Modules/_decimal/tests/bench.py in the CPython source and adapted to use
pyperf.
"""

# Original copyright notice in CPython source:

#
# Copyright (C) 2001-2012 Python Software Foundation. All Rights Reserved.
# Modified and extended by Stefan Krah.
#


import decimal


def factorial(n, m):
if n > m:
return factorial(m, n)
elif m == 0:
return 1
elif n == m:
return n
else:
return factorial(n, (n + m) // 2) * factorial((n + m) // 2 + 1, m)


def bench_decimal_factorial():
# The upstream 10,000! and 100,000! inputs are reduced for simulation,
# while retaining enough precision to exercise large Decimal arithmetic.
with decimal.localcontext() as context:
context.prec = 3000
result = factorial(decimal.Decimal(1000), 0)

assert len(result.as_tuple().digits) == 2568


bench_decimal_factorial()
71 changes: 71 additions & 0 deletions benches/benchmarks/deepcopy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Adapted from python/pyperformance 1.14.0 (deepcopy).
"""
Benchmark to measure performance of the python builtin method copy.deepcopy

Performance is tested on a nested dictionary and a dataclass

Author: Pieter Eendebak

"""
import copy
from dataclasses import dataclass


@dataclass
class A:
string: str
lst: list
boolean: bool


def benchmark_reduce(n):
""" Benchmark where the __reduce__ functionality is used """
class C(object):
def __init__(self):
self.a = 1
self.b = 2

def __reduce__(self):
return (C, (), self.__dict__)

def __setstate__(self, state):
self.__dict__.update(state)
c = C()

for _ in range(n):
_ = copy.deepcopy(c)


def benchmark_memo(n):
""" Benchmark where the memo functionality is used """
A = [1] * 100
data = {'a': (A, A, A), 'b': [A] * 100}

for _ in range(n):
_ = copy.deepcopy(data)


def benchmark(n):
""" Benchmark on some standard data types """
a = {
'list': [1, 2, 3, 43],
't': (1 ,2, 3),
'str': 'hello',
'subdict': {'a': True}
}
dc = A('hello', [1, 2, 3], True)

for _ in range(n):
for _ in range(30):
_ = copy.deepcopy(a)
for s in ['red', 'blue', 'green']:
dc.string = s
for value in range(5):
dc.lst[0] = value
for b in [True, False]:
dc.boolean = b
_ = copy.deepcopy(dc)

benchmark(1)
benchmark_reduce(5)
benchmark_memo(1)
Loading
Loading