Optimizing conditional code inside nested for-loops
up vote
2
down vote
favorite
Let's say I have some code that looks something like below.
boolean changecode = 0;
for (int i=0; i<4; i++) {
//some code
for (int j=0; j<4; j++) {
//some more code
if (changecode) {
//Code A
} else {
//Code B
}
}
}
This code would run the if-condition every time the loops were run, ending up executing it 16 times, which isn't really optimized. Now, I could of course do this:
boolean changecode = 0;
if (changecode) {
for (int i=0; i<4; i++) {
//some code
for (int j=0; j<4; j++) {
//some more code
//Code A
}
}
} else {
for (int i=0; i<4; i++) {
//some code
for (int j=0; j<4; j++) {
//some more code
//Code B
}
}
}
But i suppose this isn't really optimized either, thinking about all the repetition of code. Is there a way to make the conditional run in the outer layer and replace what code should be ran in the middle of the nested for-loop?
java for-loop optimization nested-loops
New contributor
add a comment |
up vote
2
down vote
favorite
Let's say I have some code that looks something like below.
boolean changecode = 0;
for (int i=0; i<4; i++) {
//some code
for (int j=0; j<4; j++) {
//some more code
if (changecode) {
//Code A
} else {
//Code B
}
}
}
This code would run the if-condition every time the loops were run, ending up executing it 16 times, which isn't really optimized. Now, I could of course do this:
boolean changecode = 0;
if (changecode) {
for (int i=0; i<4; i++) {
//some code
for (int j=0; j<4; j++) {
//some more code
//Code A
}
}
} else {
for (int i=0; i<4; i++) {
//some code
for (int j=0; j<4; j++) {
//some more code
//Code B
}
}
}
But i suppose this isn't really optimized either, thinking about all the repetition of code. Is there a way to make the conditional run in the outer layer and replace what code should be ran in the middle of the nested for-loop?
java for-loop optimization nested-loops
New contributor
3
In your first code, the testing of a boolean being true would be very fast.
– Scary Wombat
1 hour ago
3
Expanding on what @ScaryWombat said, if the conditional does some complex computation, hoist the result of that out into aboolean
on top, but leave theif
(which is now very cheap -- and maybe JITable) inside the (non-duplicated) loop.
– Thilo
1 hour ago
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
Let's say I have some code that looks something like below.
boolean changecode = 0;
for (int i=0; i<4; i++) {
//some code
for (int j=0; j<4; j++) {
//some more code
if (changecode) {
//Code A
} else {
//Code B
}
}
}
This code would run the if-condition every time the loops were run, ending up executing it 16 times, which isn't really optimized. Now, I could of course do this:
boolean changecode = 0;
if (changecode) {
for (int i=0; i<4; i++) {
//some code
for (int j=0; j<4; j++) {
//some more code
//Code A
}
}
} else {
for (int i=0; i<4; i++) {
//some code
for (int j=0; j<4; j++) {
//some more code
//Code B
}
}
}
But i suppose this isn't really optimized either, thinking about all the repetition of code. Is there a way to make the conditional run in the outer layer and replace what code should be ran in the middle of the nested for-loop?
java for-loop optimization nested-loops
New contributor
Let's say I have some code that looks something like below.
boolean changecode = 0;
for (int i=0; i<4; i++) {
//some code
for (int j=0; j<4; j++) {
//some more code
if (changecode) {
//Code A
} else {
//Code B
}
}
}
This code would run the if-condition every time the loops were run, ending up executing it 16 times, which isn't really optimized. Now, I could of course do this:
boolean changecode = 0;
if (changecode) {
for (int i=0; i<4; i++) {
//some code
for (int j=0; j<4; j++) {
//some more code
//Code A
}
}
} else {
for (int i=0; i<4; i++) {
//some code
for (int j=0; j<4; j++) {
//some more code
//Code B
}
}
}
But i suppose this isn't really optimized either, thinking about all the repetition of code. Is there a way to make the conditional run in the outer layer and replace what code should be ran in the middle of the nested for-loop?
java for-loop optimization nested-loops
java for-loop optimization nested-loops
New contributor
New contributor
edited 1 hour ago
New contributor
asked 1 hour ago
h7x4
134
134
New contributor
New contributor
3
In your first code, the testing of a boolean being true would be very fast.
– Scary Wombat
1 hour ago
3
Expanding on what @ScaryWombat said, if the conditional does some complex computation, hoist the result of that out into aboolean
on top, but leave theif
(which is now very cheap -- and maybe JITable) inside the (non-duplicated) loop.
– Thilo
1 hour ago
add a comment |
3
In your first code, the testing of a boolean being true would be very fast.
– Scary Wombat
1 hour ago
3
Expanding on what @ScaryWombat said, if the conditional does some complex computation, hoist the result of that out into aboolean
on top, but leave theif
(which is now very cheap -- and maybe JITable) inside the (non-duplicated) loop.
– Thilo
1 hour ago
3
3
In your first code, the testing of a boolean being true would be very fast.
– Scary Wombat
1 hour ago
In your first code, the testing of a boolean being true would be very fast.
– Scary Wombat
1 hour ago
3
3
Expanding on what @ScaryWombat said, if the conditional does some complex computation, hoist the result of that out into a
boolean
on top, but leave the if
(which is now very cheap -- and maybe JITable) inside the (non-duplicated) loop.– Thilo
1 hour ago
Expanding on what @ScaryWombat said, if the conditional does some complex computation, hoist the result of that out into a
boolean
on top, but leave the if
(which is now very cheap -- and maybe JITable) inside the (non-duplicated) loop.– Thilo
1 hour ago
add a comment |
3 Answers
3
active
oldest
votes
up vote
2
down vote
accepted
In this particular case I would say it is pretty much the same. I would always go for the cleaner code over optimization (because it will be close to none if not none). The compiler will optimize/cache the result probably and even if it tests a boolean it is an operation that doesn't add performance loss. And longer code does ;) Also you can use an interface and some OOP pattern to avoid the repetition and the if/else. But it will add extra objects probably so just use the first option.
add a comment |
up vote
2
down vote
I think the compiler will be able to optimize this away, but for the question's sake, you could do
Runnable code = changecode
? () -> codeA()
: () -> codeB();
for (int i=0; i<4; i++) {
//some code
for (int j=0; j<4; j++) {
//some more code
code.run();
}
}
4
Since the question seems to be about micro-optimizations, introducing an extra method call is maybe not the way forward (unless the compiler can remove it again)
– Thilo
1 hour ago
@Thilo afaik, method calls at this point are dirty cheap which means that it might still be faster than doing 16 conditional checks... hard to tell at such micro level
– Sarief
1 hour ago
1
@Sarief A method call is not going to be faster than checking an effectively-finalboolean
local variable (and also more difficult for an optimizer to reason about for further transformations)
– Thilo
1 hour ago
1
It's nice but for readability its better to make these methods. Otherwise the code will be very hard to read with anonymous pieces of code
– Veselin Davidov
50 mins ago
I upvoted the solution but just a little test (I am not sure if it is correct) showed that with two loops up to MaxInt and empty methods using functional interface takes 1900ms (consistent) and using if statement inside the for loop takes 1200ms. I am not saying it is 100% correct but still if we are talking for optimization this should be taken into account
– Veselin Davidov
38 mins ago
|
show 1 more comment
up vote
1
down vote
As others pointed out, boolean comparison is very cheap performance-wise. It is not worth optimizing something like this just to make the code look worse.
In my opinion it is very important not to overthink small things like this. Don't optimize it unless you see some performance problems and there is nothing else to improve.
It is worth mentioning that very often the compiler will optimize it on its own, there is no need to bother. Btw. it might be worth checking this article about dumb code: https://www.oracle.com/technetwork/articles/java/devinsight-1-139780.html
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
In this particular case I would say it is pretty much the same. I would always go for the cleaner code over optimization (because it will be close to none if not none). The compiler will optimize/cache the result probably and even if it tests a boolean it is an operation that doesn't add performance loss. And longer code does ;) Also you can use an interface and some OOP pattern to avoid the repetition and the if/else. But it will add extra objects probably so just use the first option.
add a comment |
up vote
2
down vote
accepted
In this particular case I would say it is pretty much the same. I would always go for the cleaner code over optimization (because it will be close to none if not none). The compiler will optimize/cache the result probably and even if it tests a boolean it is an operation that doesn't add performance loss. And longer code does ;) Also you can use an interface and some OOP pattern to avoid the repetition and the if/else. But it will add extra objects probably so just use the first option.
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
In this particular case I would say it is pretty much the same. I would always go for the cleaner code over optimization (because it will be close to none if not none). The compiler will optimize/cache the result probably and even if it tests a boolean it is an operation that doesn't add performance loss. And longer code does ;) Also you can use an interface and some OOP pattern to avoid the repetition and the if/else. But it will add extra objects probably so just use the first option.
In this particular case I would say it is pretty much the same. I would always go for the cleaner code over optimization (because it will be close to none if not none). The compiler will optimize/cache the result probably and even if it tests a boolean it is an operation that doesn't add performance loss. And longer code does ;) Also you can use an interface and some OOP pattern to avoid the repetition and the if/else. But it will add extra objects probably so just use the first option.
answered 1 hour ago
Veselin Davidov
5,4761515
5,4761515
add a comment |
add a comment |
up vote
2
down vote
I think the compiler will be able to optimize this away, but for the question's sake, you could do
Runnable code = changecode
? () -> codeA()
: () -> codeB();
for (int i=0; i<4; i++) {
//some code
for (int j=0; j<4; j++) {
//some more code
code.run();
}
}
4
Since the question seems to be about micro-optimizations, introducing an extra method call is maybe not the way forward (unless the compiler can remove it again)
– Thilo
1 hour ago
@Thilo afaik, method calls at this point are dirty cheap which means that it might still be faster than doing 16 conditional checks... hard to tell at such micro level
– Sarief
1 hour ago
1
@Sarief A method call is not going to be faster than checking an effectively-finalboolean
local variable (and also more difficult for an optimizer to reason about for further transformations)
– Thilo
1 hour ago
1
It's nice but for readability its better to make these methods. Otherwise the code will be very hard to read with anonymous pieces of code
– Veselin Davidov
50 mins ago
I upvoted the solution but just a little test (I am not sure if it is correct) showed that with two loops up to MaxInt and empty methods using functional interface takes 1900ms (consistent) and using if statement inside the for loop takes 1200ms. I am not saying it is 100% correct but still if we are talking for optimization this should be taken into account
– Veselin Davidov
38 mins ago
|
show 1 more comment
up vote
2
down vote
I think the compiler will be able to optimize this away, but for the question's sake, you could do
Runnable code = changecode
? () -> codeA()
: () -> codeB();
for (int i=0; i<4; i++) {
//some code
for (int j=0; j<4; j++) {
//some more code
code.run();
}
}
4
Since the question seems to be about micro-optimizations, introducing an extra method call is maybe not the way forward (unless the compiler can remove it again)
– Thilo
1 hour ago
@Thilo afaik, method calls at this point are dirty cheap which means that it might still be faster than doing 16 conditional checks... hard to tell at such micro level
– Sarief
1 hour ago
1
@Sarief A method call is not going to be faster than checking an effectively-finalboolean
local variable (and also more difficult for an optimizer to reason about for further transformations)
– Thilo
1 hour ago
1
It's nice but for readability its better to make these methods. Otherwise the code will be very hard to read with anonymous pieces of code
– Veselin Davidov
50 mins ago
I upvoted the solution but just a little test (I am not sure if it is correct) showed that with two loops up to MaxInt and empty methods using functional interface takes 1900ms (consistent) and using if statement inside the for loop takes 1200ms. I am not saying it is 100% correct but still if we are talking for optimization this should be taken into account
– Veselin Davidov
38 mins ago
|
show 1 more comment
up vote
2
down vote
up vote
2
down vote
I think the compiler will be able to optimize this away, but for the question's sake, you could do
Runnable code = changecode
? () -> codeA()
: () -> codeB();
for (int i=0; i<4; i++) {
//some code
for (int j=0; j<4; j++) {
//some more code
code.run();
}
}
I think the compiler will be able to optimize this away, but for the question's sake, you could do
Runnable code = changecode
? () -> codeA()
: () -> codeB();
for (int i=0; i<4; i++) {
//some code
for (int j=0; j<4; j++) {
//some more code
code.run();
}
}
answered 1 hour ago
daniu
6,45021633
6,45021633
4
Since the question seems to be about micro-optimizations, introducing an extra method call is maybe not the way forward (unless the compiler can remove it again)
– Thilo
1 hour ago
@Thilo afaik, method calls at this point are dirty cheap which means that it might still be faster than doing 16 conditional checks... hard to tell at such micro level
– Sarief
1 hour ago
1
@Sarief A method call is not going to be faster than checking an effectively-finalboolean
local variable (and also more difficult for an optimizer to reason about for further transformations)
– Thilo
1 hour ago
1
It's nice but for readability its better to make these methods. Otherwise the code will be very hard to read with anonymous pieces of code
– Veselin Davidov
50 mins ago
I upvoted the solution but just a little test (I am not sure if it is correct) showed that with two loops up to MaxInt and empty methods using functional interface takes 1900ms (consistent) and using if statement inside the for loop takes 1200ms. I am not saying it is 100% correct but still if we are talking for optimization this should be taken into account
– Veselin Davidov
38 mins ago
|
show 1 more comment
4
Since the question seems to be about micro-optimizations, introducing an extra method call is maybe not the way forward (unless the compiler can remove it again)
– Thilo
1 hour ago
@Thilo afaik, method calls at this point are dirty cheap which means that it might still be faster than doing 16 conditional checks... hard to tell at such micro level
– Sarief
1 hour ago
1
@Sarief A method call is not going to be faster than checking an effectively-finalboolean
local variable (and also more difficult for an optimizer to reason about for further transformations)
– Thilo
1 hour ago
1
It's nice but for readability its better to make these methods. Otherwise the code will be very hard to read with anonymous pieces of code
– Veselin Davidov
50 mins ago
I upvoted the solution but just a little test (I am not sure if it is correct) showed that with two loops up to MaxInt and empty methods using functional interface takes 1900ms (consistent) and using if statement inside the for loop takes 1200ms. I am not saying it is 100% correct but still if we are talking for optimization this should be taken into account
– Veselin Davidov
38 mins ago
4
4
Since the question seems to be about micro-optimizations, introducing an extra method call is maybe not the way forward (unless the compiler can remove it again)
– Thilo
1 hour ago
Since the question seems to be about micro-optimizations, introducing an extra method call is maybe not the way forward (unless the compiler can remove it again)
– Thilo
1 hour ago
@Thilo afaik, method calls at this point are dirty cheap which means that it might still be faster than doing 16 conditional checks... hard to tell at such micro level
– Sarief
1 hour ago
@Thilo afaik, method calls at this point are dirty cheap which means that it might still be faster than doing 16 conditional checks... hard to tell at such micro level
– Sarief
1 hour ago
1
1
@Sarief A method call is not going to be faster than checking an effectively-final
boolean
local variable (and also more difficult for an optimizer to reason about for further transformations)– Thilo
1 hour ago
@Sarief A method call is not going to be faster than checking an effectively-final
boolean
local variable (and also more difficult for an optimizer to reason about for further transformations)– Thilo
1 hour ago
1
1
It's nice but for readability its better to make these methods. Otherwise the code will be very hard to read with anonymous pieces of code
– Veselin Davidov
50 mins ago
It's nice but for readability its better to make these methods. Otherwise the code will be very hard to read with anonymous pieces of code
– Veselin Davidov
50 mins ago
I upvoted the solution but just a little test (I am not sure if it is correct) showed that with two loops up to MaxInt and empty methods using functional interface takes 1900ms (consistent) and using if statement inside the for loop takes 1200ms. I am not saying it is 100% correct but still if we are talking for optimization this should be taken into account
– Veselin Davidov
38 mins ago
I upvoted the solution but just a little test (I am not sure if it is correct) showed that with two loops up to MaxInt and empty methods using functional interface takes 1900ms (consistent) and using if statement inside the for loop takes 1200ms. I am not saying it is 100% correct but still if we are talking for optimization this should be taken into account
– Veselin Davidov
38 mins ago
|
show 1 more comment
up vote
1
down vote
As others pointed out, boolean comparison is very cheap performance-wise. It is not worth optimizing something like this just to make the code look worse.
In my opinion it is very important not to overthink small things like this. Don't optimize it unless you see some performance problems and there is nothing else to improve.
It is worth mentioning that very often the compiler will optimize it on its own, there is no need to bother. Btw. it might be worth checking this article about dumb code: https://www.oracle.com/technetwork/articles/java/devinsight-1-139780.html
add a comment |
up vote
1
down vote
As others pointed out, boolean comparison is very cheap performance-wise. It is not worth optimizing something like this just to make the code look worse.
In my opinion it is very important not to overthink small things like this. Don't optimize it unless you see some performance problems and there is nothing else to improve.
It is worth mentioning that very often the compiler will optimize it on its own, there is no need to bother. Btw. it might be worth checking this article about dumb code: https://www.oracle.com/technetwork/articles/java/devinsight-1-139780.html
add a comment |
up vote
1
down vote
up vote
1
down vote
As others pointed out, boolean comparison is very cheap performance-wise. It is not worth optimizing something like this just to make the code look worse.
In my opinion it is very important not to overthink small things like this. Don't optimize it unless you see some performance problems and there is nothing else to improve.
It is worth mentioning that very often the compiler will optimize it on its own, there is no need to bother. Btw. it might be worth checking this article about dumb code: https://www.oracle.com/technetwork/articles/java/devinsight-1-139780.html
As others pointed out, boolean comparison is very cheap performance-wise. It is not worth optimizing something like this just to make the code look worse.
In my opinion it is very important not to overthink small things like this. Don't optimize it unless you see some performance problems and there is nothing else to improve.
It is worth mentioning that very often the compiler will optimize it on its own, there is no need to bother. Btw. it might be worth checking this article about dumb code: https://www.oracle.com/technetwork/articles/java/devinsight-1-139780.html
answered 13 mins ago
Amongalen
1429
1429
add a comment |
add a comment |
h7x4 is a new contributor. Be nice, and check out our Code of Conduct.
h7x4 is a new contributor. Be nice, and check out our Code of Conduct.
h7x4 is a new contributor. Be nice, and check out our Code of Conduct.
h7x4 is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53370949%2foptimizing-conditional-code-inside-nested-for-loops%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
In your first code, the testing of a boolean being true would be very fast.
– Scary Wombat
1 hour ago
3
Expanding on what @ScaryWombat said, if the conditional does some complex computation, hoist the result of that out into a
boolean
on top, but leave theif
(which is now very cheap -- and maybe JITable) inside the (non-duplicated) loop.– Thilo
1 hour ago