3 ms·
Why is there a "continue" at all in the first code sample? Edit to add: does removing it make any difference?
by schemescape 3y ago
Why is there a "continue" at all in the first code sample?
Edit to add: does removing it make any difference?
- ludiludi 3y agoGood 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#L12 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.
- schemescape 3y agoThanks! 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...
- fmstephe 3y agoAccording to the godot compiler explorer removing the `continue` makes no difference to the generated assembly. https://godbolt.org/z/ds1raTYc9 https://godbolt.org/z/ds1raTYc9 https://godbolt.org/z/rbWsxM83b https://godbolt.org/z/rbWsxM83b The `print("lol")` output looks remarkably different. https://godbolt.org/z/c3afrb6bG https://godbolt.org/z/c3afrb6bG
- rep_lodsb 3y agoIt 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.
- nemetroid 3y agoThe 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.
- ahazred8ta 3y agounrelated: a few months ago you were asking about small language runtimes https://docs.micropython.org/en/latest/ https://docs.micropython.org/en/latest/