You could do a lot worse. If the type of i was an object, you could overload the negation operation to have side-effects for the third snippet, for example.
- 0 Posts
- 7 Comments
I don’t know what I was thinking.
But, if you borrow C’s semantics, you are allowed to “optimize” away side-effect-less loops, even if they would never terminate. But that would require the random method to be pure.
I feel like an idiot. Also, in the “Good” example, no underflow occurs. i goes from 0 to -10, and x is assigned to -i every loop.
It might still be possible to optimize away the random number example, if the random function were made a magic language item, but it would not be even remotely close to being worth the effort.
ferric_carcinization@lemmy.mlto
Programmer Humor@lemmy.ml•More code = more betterEnglish
18·2 days agoThe compiler should be able to optimize all of them to the same machine code.
- This is already good.
- Easily optimized by constant folding.
This one depends on the semantics of signed underflow, so it may not do what you want.The loop can only exit ifx==10, so as long as thenextInt()method doesn’t have side effects, the loop should be eliminated. But, again, language semantics can affect this.
Edit: Very wrong for 3 & 4, see replies.
ferric_carcinization@lemmy.mlto
DeGoogle Yourself@lemmy.ml•I had enough of big tech and their actions, so created this guide to make switching as easy as possibleEnglish
1·7 days agoI’m not Swiss, but I’ve understood that Switzerland adopts many EU laws, but not all of them. I’ve also heard that a right-wing party, which wants the country to be more independent from the EU is pretty popular there.
As this is just based on things that I’ve heard, I don’t have any sources to link.
What I mean is, Switzerland is an European country, but as I’ve understood, any specific EU law or regulation may or may not apply.
ferric_carcinization@lemmy.mlto
DeGoogle Yourself@lemmy.ml•I had enough of big tech and their actions, so created this guide to make switching as easy as possibleEnglish
5·7 days agoIn your guide, Proton’s services have the EU flag, but the company is Swiss.
If it were a magic lang item, you could treat the resulting value in a special way. Then, you could create an optimization pass for this situation: if a variable is assigned random in a loop and the loop can only be exited with a certain value, the compiler can coerce the magic rand value to it.