6 ms·
Yeah, this is usually what I run into. I once had a recursive algorithm to collect a flat list of nodes in a tree. I found that building slices was the bottlene
by weberc2 8y ago
Yeah, this is usually what I run into. I once had a recursive algorithm to collect a flat list of nodes in a tree. I found that building slices was the bottleneck, so I changed the signature to `func (t Tree) CountNodes() int` and `func (t Tree) Nodes(outSlice []Node)`, and then I'd call it like:
nodes := make([]Node, t.CountNodes())
t.Nodes(nodes)
Even though this required me to traverse the tree twice (once to count and the other to populate the list of nodes) the savings were considerable.