Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use STL bitsets instead of separate variables?

In what situation would it be more appropriate for me to use a bitset (STL container) to manage a set of flags rather than having them declared as a number of separate (bool) variables?

Will I get a significant performance gain if I used a bitset for 50 flags rather than using 50 separate bool variables?

like image 786
David Avatar asked Aug 21 '08 18:08

David


People also ask

Is Bitset faster than an array of bools?

Using Clang 10.0 and GCC 10.1, in both cases the array of bools is faster than bitset.

What does Bitset mean in C++?

Bitset is a container in C++ Standard Template Library for dealing with data at the bit level. 1. A bitset stores bits (elements with only two possible values: 0 or 1). We can however get the part of a string by providing positions to bitset constructor (Positions are with respect to string position from left to right)

What is Bitset data structure?

A bit array (also known as bit map, bit set, bit string, or bit vector) is an array data structure that compactly stores bits. It can be used to implement a simple set data structure.

How is Bitset implemented?

Implementation: The required implementation can be done easily by maintaining a string of a given size and performing all operations on it by simultaneously keeping a count of 0s and 1s in it. But the flip operation would not be possible in constant time with this approach.


2 Answers

Well, 50 bools as a bitset will take 7 bytes, while 50 bools as bools will take 50 bytes. These days that's not really a big deal, so using bools is probably fine.

However, one place a bitset might be useful is if you need to pass those bools around a lot, especially if you need to return the set from a function. Using a bitset you have less data that has to be moved around on the stack for returns. Then again, you could just use refs instead and have even less data to pass around. :)

like image 76
Herms Avatar answered Oct 01 '22 19:10

Herms


std::bitset will give you extra points when you need to serialize / deserialize it. You can just write it to a stream or read from a stream with it. But certainly, the separate bools are going to be faster. They are optimized for this kind of use after all, while a bitset is optimized for space, and has still function calls involved. It will never be faster than separate bools.

Bitset

  • Very space efficient
  • Less efficient due to bit fiddling
  • Provides serialize / de-serialize with op<< and op>>
  • All bits packed together: You will have the flags at one place.

Separate bools

  • Very fast
  • Bools are not packed together. They will be members somewhere.

Decide on the facts. I, personally, would use std::bitset for some not-performance critical, and would use bools if I either have only a few bools (and thus it's quite overview-able), or if I need the extra performance.

like image 32
Johannes Schaub - litb Avatar answered Oct 01 '22 18:10

Johannes Schaub - litb