Hacker News new | ask | show | jobs
by schemescape 1033 days ago
Why is there a "continue" at all in the first code sample?

Edit to add: does removing it make any difference?

5 comments

The inclusion of ”continue” in the non-lol version is pointless and obscures the actual reason for the difference: the addition of the non-pointless ”continue” in the lol version.

As other comments point out, this construct can be replaced by a cmov instruction:

  if a > b:
    b = a
The following construct however, cannot be replaced by cmov:

  if a > b:
    b = a
    continue
Only by first eliminating the pointless "continue" is this replacement valid. But by including it, you can make it look like it's the 'print("lol")' is what makes the difference, which is only true lexically.
According to the godot compiler explorer removing the `continue` makes no difference to the generated assembly.

https://godbolt.org/z/ds1raTYc9

https://godbolt.org/z/rbWsxM83b

The `print("lol")` output looks remarkably different.

https://godbolt.org/z/c3afrb6bG

Good question. As you can see in the comment in the github repo, it has no effect. https://github.com/ludi317/max/blob/master/blog/max_test.go#...

It is there only to match the continue in the second code sample, where it is needed.

Thanks! In that case, I have to say I'm surprised. I assumed the code generated for the loop would have an instructions that branches, so adding another branching instruction could only hurt (edit: not necessarily a lot), but apparently my intuition is wrong.

I'm curious if the performance difference noted in the article happens on Intel/AMD as well...

unrelated: a few months ago you were asking about small language runtimes https://docs.micropython.org/en/latest/
It is there to skip the print("lol") in the second version if the condition is true. Since the array is sorted in ascending order, it will be true for every value, and that print is never be executed.