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
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
3
In your first code, the testing of a boolean being true would be very fast.
– Scary Wombat
Nov 19 at 8:39
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
Nov 19 at 8:41
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
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
edited Nov 19 at 8:39
asked Nov 19 at 8:38
h7x4
135
135
3
In your first code, the testing of a boolean being true would be very fast.
– Scary Wombat
Nov 19 at 8:39
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
Nov 19 at 8:41
add a comment |
3
In your first code, the testing of a boolean being true would be very fast.
– Scary Wombat
Nov 19 at 8:39
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
Nov 19 at 8:41
3
3
In your first code, the testing of a boolean being true would be very fast.
– Scary Wombat
Nov 19 at 8:39
In your first code, the testing of a boolean being true would be very fast.
– Scary Wombat
Nov 19 at 8:39
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
Nov 19 at 8:41
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
Nov 19 at 8:41
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
Nov 19 at 8:44
@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
Nov 19 at 8:52
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
Nov 19 at 8:56
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
Nov 19 at 9:07
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
Nov 19 at 9:19
|
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 Nov 19 at 8:43
Veselin Davidov
5,5561515
5,5561515
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
Nov 19 at 8:44
@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
Nov 19 at 8:52
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
Nov 19 at 8:56
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
Nov 19 at 9:07
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
Nov 19 at 9:19
|
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
Nov 19 at 8:44
@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
Nov 19 at 8:52
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
Nov 19 at 8:56
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
Nov 19 at 9:07
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
Nov 19 at 9:19
|
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 Nov 19 at 8:41
daniu
6,69821634
6,69821634
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
Nov 19 at 8:44
@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
Nov 19 at 8:52
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
Nov 19 at 8:56
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
Nov 19 at 9:07
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
Nov 19 at 9:19
|
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
Nov 19 at 8:44
@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
Nov 19 at 8:52
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
Nov 19 at 8:56
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
Nov 19 at 9:07
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
Nov 19 at 9:19
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
Nov 19 at 8:44
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
Nov 19 at 8:44
@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
Nov 19 at 8:52
@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
Nov 19 at 8:52
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
Nov 19 at 8:56
@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
Nov 19 at 8:56
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
Nov 19 at 9:07
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
Nov 19 at 9:07
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
Nov 19 at 9:19
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
Nov 19 at 9:19
|
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 Nov 19 at 9:44
Amongalen
1429
1429
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Nov 19 at 8:39
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
Nov 19 at 8:41