• 0

Loop efficiency comparison, while(true) VS for(;;)


Question

3 answers to this question

Recommended Posts

  • 0

it's the same in my experiences.......

the only thing i can think of is that the computer doesn't have to do any work when going to the next cycle for the for(;;) loop. with the while(true) loop, in each cycle the computer has to check to see if true is ... well, true

i hope that helps with this oh-so-complicated question/task

  • 0
  Quote
this oh-so-complicated question/task

*LOL*

Personally, I think it's just that they want to look smarter. while(true) looks like a newbie mistake. for(;;) looks like you know what you're doing. I doubt either is any more efficient than the other. If the for loop actually is more efficient, the difference would be measured in thousandths (if even that much) of a second.

  • 0

Well, at least in C using gcc as the compiler, there is no difference. The assembly code output is identical.

While loop:

        .file   "infwhile.c"
        .section        .rodata
.LC0:
        .string "Hello world\n"
        .text
.globl main
        .type   main, @function
main:
        pushl   %ebp
        movl    %esp, %ebp
        subl    $8, %esp
        andl    $-16, %esp
        movl    $0, %eax
        addl    $15, %eax
        addl    $15, %eax
        shrl    $4, %eax
        sall    $4, %eax
        subl    %eax, %esp
.L2:
        movl    $.LC0, (%esp)
        call    printf
        jmp     .L2
        .size   main, .-main
        .section        .note.GNU-stack,"",@progbitsident  "GCC: (GNU) 3.4.4 (Gentoo 3.4.4-r1, ssp-3.4.4-1.0, pie-8.7.8)"

For loop:

        .file   "inffor.c"
        .section        .rodata
.LC0:
        .string "Hello world\n"
        .text
.globl main
        .type   main, @function
main:
        pushl   %ebp
        movl    %esp, %ebp
        subl    $8, %esp
        andl    $-16, %esp
        movl    $0, %eax
        addl    $15, %eax
        addl    $15, %eax
        shrl    $4, %eax
        sall    $4, %eax
        subl    %eax, %esp
.L2:
        movl    $.LC0, (%esp)
        call    printf
        jmp     .L2
        .size   main, .-main
        .section        .note.GNU-stack,"",@progbitsident  "GCC: (GNU) 3.4.4 (Gentoo 3.4.4-r1, ssp-3.4.4-1.0, pie-8.7.8)"

And the code to produce those was just

for(;;) { printf("Hello world\n"); }

while (1) { printf("Hello world\n"); }

How this works out in other languages, I can't be sure. It also depends on how the compiler interprets it. But an infinite loop is just calling the same block of code over and over. So that means once you reach the end of the code, you jump back up to the start of it, which is what we see here. There is no real difference between a for loop and a while loop in assembly, although they can differ some depending on how the compiler handles it. I've heard some people state that while loops are faster, but whether or not that's true, I don't know. It shouldn't be that way, but depending on the code and the compiler, it may be true.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • "Can" is fine. I want the ability for it to "not." Actually, I would prefer vice versa. But it has to be optional, otherwise companies wouldn't be too happy.
    • ChatGPT can now connect to Outlook, Teams, Gmail, Google Drive, and other services by Pradeep Viswanathan Apart from regular consumers, ChatGPT is also growing fast among business users. In fact, OpenAI now has 3 million paying (Enterprise, Team, and Edu) business users, up from 2 million in February. During a live stream today targeted toward business users, OpenAI announced that ChatGPT can now connect to more external services to pull in real-time context and provide more useful responses for users. The following are some of the external connectors available in ChatGPT Deep Research for Plus, Pro, Team, Enterprise, and Edu users: Microsoft Outlook Microsoft Teams Microsoft SharePoint Dropbox Box Google Drive Gmail Liner In addition to the above, IT admins in organizations can now build custom ChatGPT connectors using the popular Model Context Protocol (MCP). These custom connectors will allow organizations to make use of the data available inside their proprietary systems and other apps within ChatGPT, alongside pre-built connectors. Today, the ChatGPT team also announced a new feature called record mode for ChatGPT Team users on macOS. The record mode feature will allow users to capture meeting audio and get meeting transcriptions, meeting action items, summaries, etc. ChatGPT record mode will also be available to Plus, Pro, Enterprise, and Edu users in the future. Kevin Weil, Chief Product Officer at OpenAI, tweeted the following regarding today’s launch: While ChatGPT's new connectors and features mark significant progress, it faces a formidable challenge in the enterprise market against Microsoft 365 Copilot, which enjoys native integration within the Microsoft 365 ecosystem.
    • I decided to give this a try and, wow. I like it. One of the most customizable browsers I've ever used I think, and it even has the classic menu bar. That's one of the reasons I've stuck with Firefox for so long. Not sure if I'll make this my main browser yet but it's looking good so far.
    • No messaging app from a large commercial entity is really encrypted to a point they are actually unable to access your content. If you trust whatsapp and friends to do this either you are a fool.
    • From my point of view this seems like a good thing? Option to pay as you go in an otherwise Subscription app? Yes Please! Free Windsurf + BYOK = Now I have a reason to try Windsurf.
  • Recent Achievements

    • Apprentice
      DarkShrunken went up a rank
      Apprentice
    • Dedicated
      CHUNWEI earned a badge
      Dedicated
    • Collaborator
      DarkShrunken earned a badge
      Collaborator
    • Rookie
      Pat-Garrett went up a rank
      Rookie
    • Week One Done
      Outdoor Saunaio earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      330
    2. 2
      snowy owl
      165
    3. 3
      +FloatingFatMan
      158
    4. 4
      ATLien_0
      154
    5. 5
      Xenon
      127
  • Tell a friend

    Love Neowin? Tell a friend!