• morphballganon@lemmy.world
    link
    fedilink
    arrow-up
    0
    ·
    10 months ago

    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.

    • Knusper@feddit.de
      link
      fedilink
      arrow-up
      0
      ·
      10 months ago

      Any reasonable compiler should effectively transform them to be the same. (At the assembly level, a for-loop just uses a jump instruction, which is also what a goto effectively is.)

  • xmunk@sh.itjust.works
    link
    fedilink
    arrow-up
    0
    ·
    10 months ago

    When you can’t reasonably use a loop or when it’s not particularly important for performance and the recursive function is more readable.

    Honestly, in code the absolute most important quality is readability. Readability should always be prioritized until you find performance issues that matter in a specific block of code.

    • xxd@discuss.tchncs.de
      link
      fedilink
      English
      arrow-up
      0
      ·
      10 months ago

      On a side note: not caring about performance “until you find performance issues” is a huge problem with modern software imho. That’s the reason apps are so slow, updates take so long, everything uses so much space. I’d wish that performance would always be a point of consideration, not an afterthought once there are problems.

      • Knusper@feddit.de
        link
        fedilink
        arrow-up
        0
        ·
        10 months ago

        I feel like lots of people assumed that performance would solve itself. Hardware continuously gets faster, but even in 2023, you can usually tell that something has been implemented in Python, NodeJS, JVM languages etc., even if it’s relatively well-optimized.

        The responsiveness of C/C++, Rust etc. is still noticeably better, often without really having to optimize your application code.

      • Diplomjodler@feddit.de
        link
        fedilink
        arrow-up
        0
        ·
        10 months ago

        Those performance issues should obviously be found in testing, not after you shipped stuff. Also, if you have readable code, tuning it for performance will be much easier than fixing something that runs fast but nobody understands.