0

If I want to clear a specific boolean variable on a lot of objects, and one of these objects should get this variable set afterwards, what is most efficient:

  • iterate all objects and just set the var = false
  • iterate all objects, check the var and just set it false for the one that where true
class Foo {
   boolean bar;  //false for most objects
}

Then either use:

for (Foo foo : fooList) {
   foo.setBar(false);
}

or:

for (Foo foo : fooList) {
    if (foo.isBar()) {
       foo.setBar(false);
    }
}

then set one foo:

singleFoo.setBar(true);

Probably the time saving is minor for both implementations. But I cannot measure that atm, so I'd be interested what you thing is the better approach?

membersound
  • 81,582
  • 193
  • 585
  • 1,120
  • 2
    It would heavily depend on what you need. By the way, this is micro optimization which is the root of all evil. – Luiggi Mendoza Mar 04 '13 at 16:22
  • I agree with @LuiggiMendoza. If this is some vital code in some rendering loop that is not performing sufficiently then try both in some sample application and measure the difference. However, if your codebase is optimized such that this is the biggest problem, life is good. – Rob Goodwin Mar 04 '13 at 16:25
  • The time saved over the lifetime of the app will be less than it takes to type up an answer to this. – millimoose Mar 04 '13 at 16:51
  • Also, **always** measure first before asking a performance-related question. – millimoose Mar 04 '13 at 16:54

6 Answers6

5

Im not sure if the compiler will level these two examples out. But Id be very surprised if this was a performance bottle neck for you.

Just use this:

for (Foo foo : fooList) {
   foo.setBar(false);
}

As its easier to read

cowls
  • 24,013
  • 8
  • 48
  • 78
2

I don't know what are you trying to do and, as you said, the difference in time will be minor i.e. undetectable. , but the answer is simple: only changing the value will probably be "faster" and, as said by cowls, easier to read, so you should go for:

for (Foo foo : fooList) {
   foo.setBar(false);
}

Explaining a little bit:

I'm assuming you want to calculate future efficiency in other programs so I would recommend you calculate best, worst and average case. In this case you could calculate how much time each statement will take if you have to change all the booleans or just a few.

For example, you could say foo.setBar(false) takes A time and the if (foo.isBar()) will take B time.

Lets say you have only 3 foos in the foolist you can experience the following:

  • Worst case: you have to change bar in all 3 foos.
  • Average: sometimes 1 or 2 at most bars need to be changed.
  • Best case: no bars need to be changed.

Doing only A you will have for all 3 cases total time execution of 3A. If you do it with the if, it will take:

  • Worst case: 3A + 3B
  • Average: (1-2)A + 3B
  • Best case: 3B

If we assume B < A then the if option will only be faster in third case. If B<<A then the average will be better. But the worst case will be always worse than doing just the assignment. And in this case, probably A and B will be similar or equal so you will have (A=B):

  • Worst case: 6A
  • Average case: (4-5) A
  • Best case: 3A

So, the first option (shown at the beginning) will be the best.

This will be useful for any other language or performance issue you face. Although if you want you could review official Java docs to check whether there are special VM improvements for this.

Christian Vielma
  • 15,263
  • 12
  • 53
  • 60
1

Set them all to false and then set the single one to true. There is a great post on branch prediction I will find it.

Why is it faster to process a sorted array than an unsorted array?

If you have a large set of objects simple if statements may become a huge bottleneck. It's hard to say how much it will matter since it depends on how many of objects will be set if you add the check.

Community
  • 1
  • 1
Daniel Williams
  • 8,673
  • 4
  • 36
  • 47
0

This is the better approach

for (Foo foo : fooList) {
   foo.setBar(false);
}

Because it's clearer, and the time you will save from not writing and messing around with unclear code you can spend making actually slow code run faster, for instance.

djechlin
  • 59,258
  • 35
  • 162
  • 290
0

The most "efficient" approach is to set them all to false (your first option). The second requires two method calls as opposed to one. Of course, the difference in efficiency is almost certainly going to be immaterial. If there is any reason that you might want to know (or log) who was false before being set to false, then the later approach is obviously the way to go.

CBass
  • 983
  • 6
  • 11
0

Things you are talking about is JIT compiler level optimizations. You should not consider them while writing code. Before optimize, profile first. And start optimizing from the slowest place, then go down.

Mikhail
  • 4,175
  • 15
  • 31