Skip to content

Instantly share code, notes, and snippets.

@graingert
Created May 12, 2025 06:45
Show Gist options
  • Save graingert/e29c57cc7d5a5fec97ea5a063abdf775 to your computer and use it in GitHub Desktop.
Save graingert/e29c57cc7d5a5fec97ea5a063abdf775 to your computer and use it in GitHub Desktop.
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(1000000):
node = Node(node)
return node
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_invert_cond_return(self):
nodes = []
node: Node | None = self
while True:
if node is None:
return nodes
nodes.append(node)
node = node._parent
def ancestors_with_self_traditional_while_true_invert_cond_break(self):
nodes = []
node: Node | None = self
while True:
if node is None:
break
nodes.append(node)
node = node._parent
return nodes
def ancestors_with_self_traditional_while_true_break(self):
nodes = []
node: Node | None = self
while True:
if node is not None:
nodes.append(node)
node = node._parent
else:
break
return nodes
def ancestors_with_self_traditional_while_true_return(self):
nodes = []
node: Node | None = self
while True:
if node is not None:
nodes.append(node)
node = node._parent
else:
return nodes
return nodes
import pyperf
def main():
runner = pyperf.Runner()
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, invert cond (return)",
stmt="demo.ancestors_with_self_traditional_while_true_invert_cond_return(nodes)",
setup="import demo; nodes = demo.mk_nodes()")
runner.timeit(name="flatten a linked list tradition while true, invert cond (break)",
stmt="demo.ancestors_with_self_traditional_while_true_invert_cond_break(nodes)",
setup="import demo; nodes = demo.mk_nodes()")
runner.timeit(name="flatten a linked list tradition while true, (break)",
stmt="demo.ancestors_with_self_traditional_while_true_break(nodes)",
setup="import demo; nodes = demo.mk_nodes()")
runner.timeit(name="flatten a linked list tradition while true, (return)",
stmt="demo.ancestors_with_self_traditional_while_true_return(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
* the standard deviation (7.81 ms) is 16% of the mean (48.2 ms)

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: 48.2 ms +- 7.8 ms
.....................
WARNING: the benchmark result may be unstable
* the standard deviation (9.82 ms) is 22% of the mean (45.2 ms)
* the maximum (89.3 ms) is 98% greater than the mean (45.2 ms)

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, invert cond (return): Mean +- std dev: 45.2 ms +- 9.8 ms
.....................
WARNING: the benchmark result may be unstable
* the standard deviation (7.46 ms) is 17% of the mean (44.4 ms)

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, invert cond (break): Mean +- std dev: 44.4 ms +- 7.5 ms
.....................
WARNING: the benchmark result may be unstable
* the standard deviation (6.77 ms) is 14% of the mean (49.3 ms)
* the maximum (75.4 ms) is 53% greater than the mean (49.3 ms)

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, (break): Mean +- std dev: 49.3 ms +- 6.8 ms
.....................
WARNING: the benchmark result may be unstable
* the standard deviation (6.21 ms) is 13% of the mean (49.5 ms)

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, (return): Mean +- std dev: 49.5 ms +- 6.2 ms

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