Skip to content

Instantly share code, notes, and snippets.

@btel
Created February 16, 2026 22:19
Show Gist options
  • Select an option

  • Save btel/b1938808ff752e5f5eaf8af16f5916b3 to your computer and use it in GitHub Desktop.

Select an option

Save btel/b1938808ff752e5f5eaf8af16f5916b3 to your computer and use it in GitHub Desktop.
benchmark yaml parsers
import yaml
import ryaml
import pathlib
paths = pathlib.Path("yamls").glob("*.yaml")
def read_yaml():
nodes = []
for p in paths:
with open(p, 'rb') as fid:
node = yaml.safe_load(fid)
nodes.append(node)
return nodes
def read_ryaml():
nodes = []
for p in paths:
with open(p, 'rb') as fid:
node = ryaml.load(fid)
nodes.append(node)
return nodes
nodes = read_ryaml()
# nodes = read_yaml()
value = 0
for node in nodes:
for edge in node['edges']:
value += edge['value']
print(f"{value=}")
import string
import yaml
import uuid
import datetime
from dataclasses import dataclass, asdict
import random
@dataclass
class TreeEdge:
value: int
next_uuid: uuid.UUID | None
name: str
@dataclass
class TreeNode:
id: uuid.UUID | None
name: str
date_created: datetime.datetime
edges: list[TreeEdge]
def random_uuid():
return str(uuid.uuid4())
def random_date():
return datetime.datetime.fromordinal(random.randint(1, 3650))
def random_str():
return ''.join(random.choice(string.ascii_letters) for _ in range(10))
def random_tree(stop):
"""generate tree with randomised attributes"""
parents = [None]
n_nodes = 0
while n_nodes < stop:
next_parents = []
for parent in parents:
edges = [TreeEdge(value=random.randint(0, 100), next_uuid=random_uuid(), name=random_str()) for _ in range(10)]
yield TreeNode(
id=parent,
name=random_str(),
date_created=random_date(),
edges=edges
)
n_nodes+=1
next_parents.extend(edges)
parents = next_parents
if __name__ == "__main__":
for node in random_tree(1000):
with open("yamls/node_{}.yaml".format(node.id), "w") as f:
node_dict = asdict(node)
yaml.safe_dump(node_dict, f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment