Is using a vector of boolean values slower than a dynamic bitset?

后端 未结 4 1188
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-09 03:34

Is using a vector of boolean values slower than a dynamic bitset?

I just heard about boost\'s dynamic bitset, and I was wondering is it worth the trouble. Can I just

4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-09 03:56

    You should usually avoid std::vector because it is not a standard container. It's a packed version, so it breaks some valuable guarantees usually given by a vector. A valid alternative would be to use std::vector which is what Herb Sutter recommends.

    You can read more about it in his GotW on the subject.

    Update:

    As has been pointed out, vector can be used to good effect, as a packed representation improves locality on large data sets. It may very well be the fastest alternative depending on circumstances. However, I would still not recommend it by default since it breaks many of the promises established by std::vector and the packing is a speed/memory tradeoff which may be beneficial in both speed and memory.

    If you choose to use it, I would do so after measuring it against vector for your application. Even then, I'd recommend using a typedef to refer to it via a name which does not seem to make the guarantees which it does not hold.

提交回复
热议问题