▲ | CyberDildonics 2 days ago | |||||||||||||||||||||||||
Prove it. You can put your stack data structure on the stack anyway. A balanced tree isn't going to have more depth than your memory address bit length. Why would copying a single value be slower than pushing an entire call frame to the stack? Locality is what matters and there is no truth to what you're saying. More important is the debugability. If you have a normal data structure you can see the full stack of values. If you use recursion you have to unwind through multiple call frames and look at each one individually. Recursion is for people who want to show a neat clever trick, it isn't the best way to program. | ||||||||||||||||||||||||||
▲ | fooker a day ago | parent [-] | |||||||||||||||||||||||||
Here you go : https://godbolt.org/z/haroWGa3c Recursive DFS took 4 ms Iterative DFS took 8 ms I'm sure you could optimize the explicit stack based one a bit more to reach parity with a significantly more complex program. But might as well let ~75 years of hardware, OS, and compiler advancements do that for you when possible. > Why would copying a single value be slower than pushing an entire call frame to the stack Because that's not what happens. The stack arithmetic is handled in hardware increasing IPC significantly, and the 'frame' you are talking about it almost the same same size as a single value in the happy path when all the relevant optimizations work out. > More important is the debugability Debugging recursive programs is pretty neat with most debuggers. No, you don't unwind through anything manually, just generate a backtrace. | ||||||||||||||||||||||||||
|