# Really strange output difference between 2 loops

**URL:** <https://users.rust-lang.org/t/really-strange-output-difference-between-2-loops/140250>\
**Category:** uncategorized\
**Created:** [May 25, 2026, 8:03am UTC](https://users.rust-lang.org/t/really-strange-output-difference-between-2-loops/140250 "2026-05-25T08:03:14Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![NewUser](https://avatars.discourse-cdn.com/v4/letter/n/3e96dc/32.png) [@NewUser](https://users.rust-lang.org/u/NewUser)\
**Post date:** [May 25, 2026, 8:03am UTC](https://users.rust-lang.org/t/really-strange-output-difference-between-2-loops/140250/1 "2026-05-25T08:03:14Z")

</div>

Hi,  
I was trying a useless piece of code to see the difference between the treatment of ranges, either inclusive or exclusive.  
[Godbolt test](https://godbolt.org/z/EY1f6q93T)

(I lazily reused the default function names, "square").

The assembly difference looks really strange to me. The code does the exact same useless thing, but the assembly efficiency varies a lot.  
I don't care if it's not measurable in real life, I focus on the output and don't understand why it's so different.

Is it a stabilization issue?

Thanks guys!

---

<div class="post-metadata">

**Author:** ![bjorn3](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/bjorn3/32/5255_2.png) [@bjorn3](https://users.rust-lang.org/u/bjorn3)\
**Post date:** [May 25, 2026, 10:18am UTC](https://users.rust-lang.org/t/really-strange-output-difference-between-2-loops/140250/2 "2026-05-25T10:18:17Z")

</div>

`..=` is known to be less efficient. It has to handle the case where the start and end bounds are equal which should return a single value. For `..` this would just be an empty range, while for `..=` there needs to be an additional bool field to distinguish between a range with a single element remaining and an exhausted range. It is not possible to just increment the end of the range by a single element and then handle it identical to `..`, because the increment can overflow.

---

<div class="post-metadata">

**Author:** ![StefanSalewski](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/stefansalewski/32/36472_2.png) [@StefanSalewski](https://users.rust-lang.org/u/StefanSalewski)\
**Post date:** [May 25, 2026, 11:11am UTC](https://users.rust-lang.org/t/really-strange-output-difference-between-2-loops/140250/3 "2026-05-25T11:11:38Z")

</div>

> [@bjorn3](#):
>
> `..=` is known to be less efficient.

Yes. But my feeling was, that for this case, the loop with inclusive range `..=` generates more compact code. I would have expected the opposite, even when compact code does not always mean fast code. The generated code for the first exclusive test with `..` is quite verbose.

---

<div class="post-metadata">

**Author:** ![geebee22](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/geebee22/32/16527_2.png) [@geebee22](https://users.rust-lang.org/u/geebee22)\
**Post date:** [May 25, 2026, 11:41am UTC](https://users.rust-lang.org/t/really-strange-output-difference-between-2-loops/140250/4 "2026-05-25T11:41:26Z")

</div>

I was going to say something similar, then decided I wasn't competent. It might be because square2 is called 2nd, so doesn't need to save registers [Edit: no that isn't it]. But trying to figure something like this out is rather difficult!

---

<div class="post-metadata">

**Author:** ![Morgane55440](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/morgane55440/32/50376_2.png) [@Morgane55440](https://users.rust-lang.org/u/Morgane55440)\
**Post date:** [May 25, 2026, 12:08pm UTC](https://users.rust-lang.org/t/really-strange-output-difference-between-2-loops/140250/5 "2026-05-25T12:08:43Z")

</div>

> [@NewUser](#):
>
> Is it a stabilization issue?

what do you mean by "stabilization" and why would it matter here ?

> [@NewUser](#):
>
> I don't care if it's not measurable in real life,

who said it would not be measurable ?  
you should measure if you want to know if it is measurable or not

my banch for 100k items give me this

```rust
square_bench: 124.876ms ± 25.961
square2_bench: 156.820ms ± 41.331

```

that means the one with `..=` is 25% slower, which is what we expected because `..=` is slower in general, as @bjorn3 explained.

so the reason why the code is longer is to do more optimizations to go faster.

---

<div class="post-metadata">

**Author:** ![ProgramCrafter](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/programcrafter/32/45916_2.png) [@ProgramCrafter](https://users.rust-lang.org/u/ProgramCrafter)\
**Post date:** [May 25, 2026, 4:57pm UTC](https://users.rust-lang.org/t/really-strange-output-difference-between-2-loops/140250/6 "2026-05-25T16:57:19Z")

</div>

The first code looks like it speeds up the loop by handling `i` and `i+1` at once; did not run gdb to confirm or invalidate it tho.

---

<div class="post-metadata">

**Author:** ![curoli](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/curoli/32/52712_2.png) [@curoli](https://users.rust-lang.org/u/curoli)\
**Post date:** [May 26, 2026, 8:41am UTC](https://users.rust-lang.org/t/really-strange-output-difference-between-2-loops/140250/7 "2026-05-26T08:41:05Z")

</div>

I think you mean the case where the upper bound is equal to the largest possible value (regardless of the lower bound), which needs to be a special case for `..=`, but works like any other for `..`.

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)\
**Post date:** [August 24, 2026, 8:41am UTC](https://users.rust-lang.org/t/really-strange-output-difference-between-2-loops/140250/8 "2026-08-24T08:41:36Z")

</div>

Closed on August 24, 2026, 8:41am UTC

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.
