# Thread: Java Optimization/Boolean Logic Test

1. Fastest optimization (or implementation) wins, you can use this to test your Boolean logic skills in Java. I will post my solution when its all done, good luck!

Code:
``````	public boolean a(boolean b, boolean c, boolean d, boolean e)
{
if (b && d)
e = false;

if (b)
{
if (e && d)
b = true;

while (c)
{
if (b)
c = e;
else
b = d;

if (b)
if (e)
e = false;
else
b = e;

if (d)
{
if (e)
{
if (d)
d = c;
else
d = b;

e = d;
}
if (d && e)
{
c = e;
e = false;
if (c)
d = true;
else
d = false;

}
}
if (e || d)
{
e = false;
if (b)
e = true;

b = false;
if (e)
d = b;

int h = f(e, d);
if (h == 2)
{
if (!d && e)
h = 0;

}
if (h == 3)
{
if (c)
e = d;
else
e = c;

}
else if (h == 4)
{
if (e)
c = e;
else
c = d;

}
else if (h == 1)
{
if (b)
h += h;

if (e)
h -= h;

if (h == 2)
b = c;

}
if (h == 0)
{
for (int g = 0; g < h; g++)
{
boolean i = c;
b = c;
d = b;
if (g == 1)
c = e;
else
c = i;

}

if (h == 0)
if (c)
e = b;
else
c = e;

if (e)
d = false;

}
}
else
{
if (b)
b = e;
else
b = c;

if (c)
c = b;

if (b)
b = c;

int j = f(c, b);
if (j == 1 || j == 3)
e = d;

}
}
}
if (c && b)
{
d = e;
e = false;
}
return b;
}

public int f(boolean a, boolean b)
{
if (!a && !b)
return 0;

if (!a && b)
return 1;

if (a && !b)
return 2;

if (a && b)
return 3;

return 4;
}``````

2. Holy fuck the naming.

3. ## Thankful user:

4. He thinks he's cool cause he writes like an obfuscator.

5. ## Thankful users:

6. I did the second one,

Code:
``````public int f(boolean a, boolean b) {
if(a)
return if(b) ? 3 : 2;
else
return if(b) ? 1 : 0;
}``````

I'm doing the first one, looks like a bitch

7. Benchmark it, lol.

Lines of code are irrelevant; it's still put the same into bytecode.

8. Originally Posted by Benjii
I did the second one,

Code:
``````public int f(boolean a, boolean b) {
if(a)
return if(b) ? 3 : 2;
else
return if(b) ? 1 : 0;
}``````

I'm doing the first one, looks like a bitch
Lol that wouldn't even work.

And if you really don't want many lines then:

Code:
``````public int f(boolean a, boolean b) {
return a ? (b ? 3 : 2) : (b ? 1 : 0);
}``````
But as said before, amount of lines don't matter.

9. Your right, mine should be

Code:
``````public int f(boolean a, boolean b) {
if(a)
return b ? 3 : 2;
else
return b ? 1 : 0;
}``````
and your right again, trying to get it down to however many lines doesn't improve performance

10. Warmup...
(Joe) Test 0...Took 7785.214
(Me ) Test 1...Took 4165.532
Code:
``````import static java.lang.System.*;

public class NewMain {

public static void main(String[] args) {

int tests = 100000000;
boolean b0 = false, b1 = true;

out.println("Warmup...");
for (int i = 0; i < tests; i++) {

for (int j = 0; j < 3; j++) {
b0 = !b0;
for (int l = 0; l < 3; l++) {
b1 = !b1;
test1(b0, b1);
test0(b0, b1);
}
}
}

long start;

out.print("Test 0...");
start = System.nanoTime();
for (int i = 0; i < tests; i++) {

for (int j = 0; j < 3; j++) {
b0 = !b0;
for (int l = 0; l < 3; l++) {
b1 = !b1;
test0(b0, b1);
}
}
}
out.println("Took " + (System.nanoTime() - start) / 1000000F);

out.print("Test 1...");
start = System.nanoTime();
for (int i = 0; i < tests; i++) {

for (int j = 0; j < 3; j++) {
b0 = !b0;
for (int l = 0; l < 3; l++) {
b1 = !b1;
test1(b0, b1);
}
}
}
out.println("Took " + (System.nanoTime() - start) / 1000000F);
}

public static final int test1(boolean a, boolean b) {

if (a) {
if (b) {
return 3;
}
return 2;

} else if (b) {
return 1;
}
return 0;
}

public static final int test0(boolean a, boolean b) {
if (!a && !b) {
return 0;
}

if (!a && b) {
return 1;
}

if (a && !b) {
return 2;
}

if (a && b) {
return 3;
}

return 4;
}
}``````

11. You know if I where you I would be focusing on the first method, lol.

12. Originally Posted by Col1337
Code:
``````import static java.lang.System.*;

public class NewMain {

public static void main(String[] args) {

int tests = 100000000;
boolean b0 = false, b1 = true;

out.println("Warmup...");
for (int i = 0; i < tests; i++) {

for (int j = 0; j < 3; j++) {
b0 = !b0;
for (int l = 0; l < 3; l++) {
b1 = !b1;
test1(b0, b1);
test0(b0, b1);
}
}
}

long start;

out.print("Test 0...");
start = System.nanoTime();
for (int i = 0; i < tests; i++) {

for (int j = 0; j < 3; j++) {
b0 = !b0;
for (int l = 0; l < 3; l++) {
b1 = !b1;
test0(b0, b1);
}
}
}
out.println("Took " + (System.nanoTime() - start) / 1000000F);

out.print("Test 1...");
start = System.nanoTime();
for (int i = 0; i < tests; i++) {

for (int j = 0; j < 3; j++) {
b0 = !b0;
for (int l = 0; l < 3; l++) {
b1 = !b1;
test1(b0, b1);
}
}
}
out.println("Took " + (System.nanoTime() - start) / 1000000F);
}

public static final int test1(boolean a, boolean b) {

if (a) {
if (b) {
return 3;
}
return 2;

} else if (b) {
return 1;
}
return 0;
}

public static final int test0(boolean a, boolean b) {
if (!a && !b) {
return 0;
}

if (!a && b) {
return 1;
}

if (a && !b) {
return 2;
}

if (a && b) {
return 3;
}

return 4;
}
}``````
i, j, k, l