• 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

    • iOS 26 leaks reveal AI-powered Messages, animated Music lock screen, and CarPlay redesign by Sagar Naresh Bhavsar A lot of these are reportedly changing this year for Apple. While the "Plus" model is expected to be replaced with the iPhone 17 Air, the iOS software is also expected to undergo a major branding change. Instead of iOS 19, Apple is rumored to introduce iOS 26 at the upcoming WWDC 2025 event on June 9. Not only iOS, but the change is expected across all platforms. Meaning, from this year, it could all be streamlined: iOS 26, iPadOS 26, macOS 26, watchOS 26, and more. However, since Apple Intelligence is still in its early stages, Apple is expected to introduce a slew of new features to apps such as Music, Messages, Notes, and CarPlay. According to 9to5Mac, Apple is preparing some "low-profile enhancements" for everyday apps, based on the information from previously accurate sources. As per the report, the Messages app could get an "Automatic Translation" feature that will make use of AI (artificial intelligence) to translate both incoming and outgoing messages. Plus, the Messages app could also gain a "Polls" feature, letting group chat members vote directly within the app. Apple Music is also expected to get a new full-screen animated lock screen artwork, enhancing the Now Playing widget by bringing a maximized look to the album art on the lock screen. The Notes app could also gain the ability to export notes in Markdown, a long-requested feature that third-party apps have supported for years. Apple recently introduced CarPlay Ultra, with some amazing changes, but that is limited to a few luxury cars at the moment. The standard CarPlay is expected to get a UI overhaul with iOS 26. While details remain under wraps, the new interface is speculated to reflect the sleek, glass-like design of iOS 26. Since these are still rumors, we suggest you take them with a pinch of salt. If you are confused about which iPhones will support iOS 26, you can check out the list of supported phones here.
    • Greetings!
    • Hmm, I'll give it a go
    • Is Samsung teasing a Galaxy Z Fold7 Ultra? An official press release suggests so by Sagar Naresh Bhavsar We are inching closer to the launch of this year's premium foldables from Samsung: the Galaxy Z Fold7 and the Galaxy Z Flip7. Recently, a certification revealed that the affordable Galaxy Z Flip7 FE, could also debut alongside the other two foldables. The device is expected to take on the standard Moto Razr 2025. While we were getting excited about the trio, Samsung has put out a press release to create some hype around the upcoming foldables. Interestingly, the company has put great emphasis on the "Ultra" branding, which it usually uses for its Galaxy S-series phone and previously for Note-series phones. So, does this mean, we are getting a Galaxy Z Fold7 Ultra? Time will only tell. As for the press release, it is titled "Meet the Next Chapter of Ultra" and the first line highlights how Samsung has been listening to fans that have demanded "bigger screens, better cameras and new ways to connect and create." To churn up up the hype, Samsung added, "That’s why Galaxy’s next chapter is to provide an experience that seamlessly blends artistry and engineering to elevate everyday interactions." The GIF inside the press-release reveals what looks like the Galaxy Z Fold7 (or the Galaxy Z Fold7 Ultra, if that exists). Rumors have indicated that this year's Galaxy Z Fold7 is getting taller and wider than last year's model, thanks to a bigger display. The Z Fold7 could also feature a titanium backplate to not only reduce its thickness and make it stronger but to shed some weight as well. The company then directs the attention to its AI features such as voice controls for finding the perfect eatery or shopping place, using powerful AI-powered camera features, and so on. We will get to know more about the devices in the coming weeks. By that time, let us know your thoughts, on whether you would like an Ultra foldable or want Samsung to tweak the existing model and keep the space less-crowded.
    • A couple of friends of mine have been building Gunpla for years and got me interested, so I asked and they recommended this as a fairly good quality, very affordable, starting point. https://www.amazon.co.uk/dp/B0BGN9K1MV It was fun to build, didn't take too long, and helped me decide if I wanted to go further with the hobby, which I did.  Still only got this one built, but that's only due to time availability!
  • Recent Achievements

    • Week One Done
      Leonard grant earned a badge
      Week One Done
    • One Month Later
      portacnb1 earned a badge
      One Month Later
    • Week One Done
      portacnb1 earned a badge
      Week One Done
    • First Post
      m10d earned a badge
      First Post
    • Conversation Starter
      DarkShrunken earned a badge
      Conversation Starter
  • Popular Contributors

    1. 1
      +primortal
      260
    2. 2
      snowy owl
      158
    3. 3
      +FloatingFatMan
      145
    4. 4
      ATLien_0
      140
    5. 5
      Xenon
      131
  • Tell a friend

    Love Neowin? Tell a friend!