jackpot@lemmy.ml to Programming@programming.dev · 10 months agowhen is it best to use a recursive function vs a for loopmessage-squaremessage-square31fedilinkarrow-up132
arrow-up130message-squarewhen is it best to use a recursive function vs a for loopjackpot@lemmy.ml to Programming@programming.dev · 10 months agomessage-square31fedilink
minus-squaremorphballganon@lemmy.worldlinkfedilinkarrow-up1arrow-down14·10 months agoOne of my big takeaways from CS classes was that goto loops are more efficient than for loops. So I’d start with comparing that with a recursive function.
minus-squareLmaydev@programming.devlinkfedilinkarrow-up8·edit-210 months agoGotos are generally avoided wherever possible in modern programming. They make the code a lot harder to read and reason about. With modern compilers I seriously doubt there’s a huge difference. But generally you want your code to be readable first and optimised where needed. https://stackoverflow.com/questions/2288856/when-implementing-an-infinite-loop-is-there-a-difference-in-using-while1-vs-f/2288893#2288893
One of my big takeaways from CS classes was that goto loops are more efficient than for loops. So I’d start with comparing that with a recursive function.
Gotos are generally avoided wherever possible in modern programming.
They make the code a lot harder to read and reason about.
With modern compilers I seriously doubt there’s a huge difference.
But generally you want your code to be readable first and optimised where needed.
https://stackoverflow.com/questions/2288856/when-implementing-an-infinite-loop-is-there-a-difference-in-using-while1-vs-f/2288893#2288893