32

I'm already aware of the loop example below

bool[] switches = new bool[20];
for (int i = 0; i < switches.Length; i++) { switches[i] = false; }

But is there a more efficient way to set the entire array to false?

To explain myself a bit, i'm not, like the example above, setting a new bool array to false as it would already be false anyway. In my case i'm reading a large portion of a process memory which could fire about 18724 times at most as it is searching for patterns. When it determines that the current bytes doesn't contain the pattern, it sets the entire bool array to false along with a few other things then reads the next memory block and restarts the loop.

Although its more out of curiosity that I asked this question because the whole process still takes less than a second.

So again, my question is, is there a better way to set the entire bool array to false?

JMC17
  • 371
  • 1
  • 3
  • 9
  • 10
    Just assign it to a new array: `switches = new bool[20]`, by default all items will be false. Although this won't work if references to the array are handed out to other places, as this only affects the reference you set. No idea if it's faster or not, and does potentially put pressure on GC. – Adam Houldsworth Dec 13 '13 at 11:56
  • 2
    Almost certainly not. The process of the loop is going to need to be done regardless. – stevepkr84 Dec 13 '13 at 11:56
  • 1
    @user2025312: There are [definite possibilities for optimization](http://stackoverflow.com/questions/20565894/setting-entire-bool-to-false#comment30760980_20565939). – Jon Dec 13 '13 at 12:33

6 Answers6

67

default(bool) is false, just create the array and each element will be false.

bool[] switches = new bool[20];
Felipe Oriani
  • 37,948
  • 19
  • 131
  • 194
19

If you can re-init array, then answer of Fabian Bigler is the right way to go.

If, however, you want to re-init it to true, then use Enumerable.Repeat:

switches = Enumerable.Repeat(true, 20).ToArray();
Sinatr
  • 20,892
  • 15
  • 90
  • 319
10

You can try Array.Clear as an alternative:

Array.Clear(switches, 0, switches.Length);

Not sure what the relative performance will be, but you should definitely not expect miracles.

Jon
  • 428,835
  • 81
  • 738
  • 806
4

I believe the default value of a boolean is false. However as a sanity check, it makes sense to iterate through it. AFAIK, that is the fastest way

See: How to populate/instantiate a C# array with a single value?

Community
  • 1
  • 1
Daniel Kotin
  • 326
  • 2
  • 8
3

Is there a better way to set the entire bool array to false?

No, there is not.

You could benchmark, if assigning a new array is faster, but I doubt it. Of course, this would be done as pointed out by Adam Houldsworth.

switches = new bool[20];
Fabian Bigler
  • 10,403
  • 6
  • 47
  • 70
  • If there is no better way what about `Array.Clear()`? –  Dec 13 '13 at 12:03
  • 1
    What do you think will Array.Clear() do in the background? ;-) – Fabian Bigler Dec 13 '13 at 12:04
  • @FabianBigler To answer your reply in the deleted answer, it was a tongue-in-cheek comment. You should definitely not use linq just because you can lol – Adam Houldsworth Dec 13 '13 at 12:04
  • @AdamHouldsworth Ah well in the written language it's sometimes hard to catch the irony. ;-) – Fabian Bigler Dec 13 '13 at 12:05
  • @AdamHouldsworth And also, I did not downvote your answer anyway. ;-) – Fabian Bigler Dec 13 '13 at 12:08
  • @FabianBigler Sorry Seems I have a different definition of **better** –  Dec 13 '13 at 12:09
  • @ChristmasUnicorn Of Course Array.Clear() looks cleaner, but at the backend more has to be done. If performance REALLY matters, I would suggest as in my answer. – Fabian Bigler Dec 13 '13 at 12:11
  • I like the straight answer, i'm quite happy with all the other answers too! I didn't know about Array.Clear(); It seems the for(){} loop is the best way to go although I haven't tried benchmarking it. – JMC17 Dec 13 '13 at 12:12
  • @FabianBigler: I have no idea if this is what actually happens, but "at the backend more has to be done" is kind of shortsighted. There are valid optimizations that can be performed after the type of the array is determined to be `bool[]`. Setting multiple values with one "wide" CPU instruction is an obvious candidate; not only would it be less instructions for the CPU but it would also count as loop unrolling -- another well-known optimization. Again, I do not *know* if this happens in practice but the potential is clearly there. – Jon Dec 13 '13 at 12:31
  • @FabianBigler: Also, it's hard to see why `new bool[20]` would be faster than other solutions: it has to zero out the memory anyway *and* it has to go through the memory allocator. – Jon Dec 13 '13 at 12:32
2

switches.Clear(); will do the work. Arrays in .NET have fixed size, that's why Clear() will not remove any element, it will set all elements to default values instead (for bool this is false)

nativehr
  • 1,131
  • 6
  • 16