▲ | fooker a day ago | ||||||||||||||||
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. | |||||||||||||||||
▲ | CyberDildonics 15 hours ago | parent [-] | ||||||||||||||||
One reason it's slower is because your stack doesn't reserve any memory and grows to 40441 at its max size then shrinks back down again. Stack uses a dequeue by default which stores elements in chunks which likely causes lots of memory allocations (and deallocations) which don't happen in the recursive version. Also at n=80,000 your recursive version blows the stack. https://en.cppreference.com/w/cpp/container/deque.html 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. The program stack isn't magically special, it isn't going to beat writing a single value to memory, especially if that memory is some constant sized array already on the stack. Debugging recursive programs is pretty neat with most debuggers. No, you don't unwind through anything manually, just generate a backtrace. No matter what kind of debugger it is you're still going to be looking at a lot of information that contains the values you're looking for instead of just looking at the values directly in an array. Recursion gets used because it's quick, dirty and clever, not because it's the best way to do it. | |||||||||||||||||
|