.NET Performance: Deep Recursion vs Queue

I am writing a component that should walk along large object graphs, sometimes at 20-30 levels.

What is the most effective way to view a chart?

and. Running "steps" to avoid deep recursion

or

B. DFS (depth first level search), which can take many levels and have a deep stack trace at times.

I assume that the question I ask is this: is there any performance in .NET for running DFS that causes a "deep" stack trace? If so, what is the blow? And would I be better off with some BFS using queues that would be recursively processed in DFS?

Sorry if I do not understand. Thank.

+3
source share
3 answers

There is nothing in the runtime environment, which will lead to the fact that code execution in the deep stack will be slower than in the stack of small calls. There is no reason for this, because usually the execution of the code does not cause the stack to move.

When I speak usually, it means that there are some exceptions:

  • Security checks / evidence checks usually go to the call stack to determine the current security context.
  • Of course, throw and catch exceptions (you still don’t want to do this bypassing the tree, except interrupting it)

, , .NET , Big-O ( ), . , , , ... , Tailcall .

, Big-O, .

+4

. .NET/# ?. -,.NET x86-64, , , .

+1

, :

  • , .

  • .

, . , , .

Use the FIFO queue if you want to get the width and first LIFO if you want to get the depth first. In any case, you save the function call overhead (perhaps quite insignificant, and the call methods in the queue can be more expensive if they do not penetrate the optimizer) and the use of stack space in the system call stack.

+1
source

Source: https://habr.com/ru/post/1785676/


All Articles