Skip to content

Instantly share code, notes, and snippets.

@jcrist
jcrist / bench_init.py
Created January 23, 2023 17:14
A benchmark comparing init performance of various dataclass-like libraries
"""A quick benchmark comparing how quickly `__init__` with default values runs
for various dataclass-like libraries.
We also compare against the time it takes to initialize a `dict` or `tuple`
with the same data, as a "low-bar" for pure-python implementations.
"""
import timeit
import attrs
#![feature(conservative_impl_trait)]
fn take<'a>(take: &'a str) -> impl Fn(&'a str) -> (&'a str, &'a str) {
move |input| {
let len = input.chars().zip(take.chars()).take_while(|&(i, t)| i == t).count();
(&input[0..len], &input[len..])
}
}
fn chain<'a, F1, F2>(first: F1, second: F2) -> impl Fn(&'a str) -> ((&'a str, &'a str), &'a str)