Skip to content

Instantly share code, notes, and snippets.

@graingert
Created May 11, 2025 07:06
Show Gist options
  • Save graingert/23d43b7fb7c8d5663cc355e8390f19d9 to your computer and use it in GitHub Desktop.
Save graingert/23d43b7fb7c8d5663cc355e8390f19d9 to your computer and use it in GitHub Desktop.
pyperf flatten linked list
import sys
from typing import Self, cast
import dataclasses
@dataclasses.dataclass(slots=False)
class Node:
_parent: Self | None
MessagePump = Node
DOMNode = Node
def mk_nodes():
node = Node(None)
for i in range(10000):
node = Node(node)
return node
def ancestors_with_self_orig(self):
nodes: list[MessagePump | None] = [self]
add_node = nodes.append
node: MessagePump | None = self
while (node := node._parent) is not None:
add_node(node)
return cast("list[DOMNode]", nodes)
def ancestors_with_self_orig_repeat_append_lookup(self):
nodes: list[MessagePump | None] = [self]
node: MessagePump | None = self
while (node := node._parent) is not None:
nodes.append(node)
return cast("list[DOMNode]", nodes)
def ancestors_with_self_traditional(self):
nodes = []
node: Node | None = self
while node is not None:
nodes.append(node)
node = node._parent
return nodes
def ancestors_with_self_traditional_while_true(self):
nodes = []
node: Node | None = self
while True:
if node is None:
return nodes
nodes.append(node)
node = node._parent
import pyperf
def main():
runner = pyperf.Runner()
runner.timeit(name="flatten a linked list with a walrus",
stmt="demo.ancestors_with_self_orig(nodes)",
setup="import demo; nodes = demo.mk_nodes()")
runner.timeit(name="flatten a linked list with a walrus, nodes.append(node)",
stmt="demo.ancestors_with_self_orig_repeat_append_lookup(nodes)",
setup="import demo; nodes = demo.mk_nodes()")
runner.timeit(name="flatten a linked list tradition",
stmt="demo.ancestors_with_self_traditional(nodes)",
setup="import demo; nodes = demo.mk_nodes()")
runner.timeit(name="flatten a linked list tradition while true",
stmt="demo.ancestors_with_self_traditional_while_true(nodes)",
setup="import demo; nodes = demo.mk_nodes()")
if __name__ == "__main__":
sys.exit(main())
@graingert
Copy link
Author

.....................
WARNING: the benchmark result may be unstable
* Not enough samples to get a stable result (95% certainly of less than 1% variation)

Try to rerun the benchmark with more runs, values and/or loops.
Run 'python -m pyperf system tune' command to reduce the system jitter.
Use pyperf stats, pyperf dump and pyperf hist to analyze results.
Use --quiet option to hide these warnings.

flatten a linked list with a walrus: Mean +- std dev: 386 us +- 9 us
.....................
WARNING: the benchmark result may be unstable
* Not enough samples to get a stable result (95% certainly of less than 1% variation)

Try to rerun the benchmark with more runs, values and/or loops.
Run 'python -m pyperf system tune' command to reduce the system jitter.
Use pyperf stats, pyperf dump and pyperf hist to analyze results.
Use --quiet option to hide these warnings.

flatten a linked list with a walrus, nodes.append(node): Mean +- std dev: 334 us +- 4 us
.....................
WARNING: the benchmark result may be unstable
* Not enough samples to get a stable result (95% certainly of less than 1% variation)

Try to rerun the benchmark with more runs, values and/or loops.
Run 'python -m pyperf system tune' command to reduce the system jitter.
Use pyperf stats, pyperf dump and pyperf hist to analyze results.
Use --quiet option to hide these warnings.

flatten a linked list tradition: Mean +- std dev: 375 us +- 18 us
.....................
WARNING: the benchmark result may be unstable
* Not enough samples to get a stable result (95% certainly of less than 1% variation)

Try to rerun the benchmark with more runs, values and/or loops.
Run 'python -m pyperf system tune' command to reduce the system jitter.
Use pyperf stats, pyperf dump and pyperf hist to analyze results.
Use --quiet option to hide these warnings.

flatten a linked list tradition while true: Mean +- std dev: 331 us +- 10 us

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment