user3488765 - 8 months ago 65

C# Question

I'm doing a bit of optimization and found out that this:

`for (int x = -1; x < 2; x++)`

{

for (int y = -1; y < 2 ; y++)

{

if (((x * x) ^ (y * y)) != 1)

{

continue;

}

}

}

takes more than twice as long to run as this:

`for (int x = -1; x < 2; x++)`

{

for (int y = -1; y < 2 && ((x * x) ^ (y * y)) == 1; y++)

{

}

}

What is going on here?

The purpose is avoiding diagonal(-1,-1; -1,1 ...) and the origin(0,0) fields

Answer

The second code ends the inner loop when `((x * x) ^ (y * y)) != 1`

, whereas the first code only skips one iteration of the inner loop. Since they don't do the same thing, all bets are off on comparing performance.

Source (Stackoverflow)