Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vector<bool> access

I profiled my code using gprof and from the report, most, if not all of the top 20 or so things are about vector

Flat profile:

Each sample counts as 0.01 seconds.
  %   cumulative   self              self     total           
 time   seconds   seconds    calls  ms/call  ms/call  name    
 14.71      0.05     0.05  3870399     0.00     0.00  std::vector<bool, std::allocator<bool> >::size() const
 11.76      0.09     0.04 10552897     0.00     0.00  std::_Bit_reference::_Bit_reference(unsigned long*, unsigned long)
 11.76      0.13     0.04  7890323     0.00     0.00  std::_Bit_const_iterator::_Bit_const_iterator(std::_Bit_iterator const&)
  5.88      0.15     0.02 10089215     0.00     0.00  std::_Bit_iterator::operator*() const
  5.88      0.17     0.02  6083600     0.00     0.00  std::vector<bool, std::allocator<bool> >::operator[](unsigned int)
  5.88      0.19     0.02  3912611     0.00     0.00  std::vector<bool, std::allocator<bool> >::end() const
  5.88      0.21     0.02                             std::istreambuf_iterator<char, std::char_traits<char> > std::num_get<char, std::istreambuf_iterator<char, std::char_traits<char> > >::_M_extract_int<unsigned long long>(std::istreambuf_iterator<char, std::char_traits<char> >, std::istreambuf_iterator<char, std::char_traits<char> >, std::ios_base&, std::_Ios_Iostate&, unsigned long long&) const
  2.94      0.22     0.01  6523499     0.00     0.00  std::_Bit_reference::operator bool() const
  2.94      0.23     0.01  3940406     0.00     0.00  std::vector<bool, std::allocator<bool> >::begin() const
  2.94      0.24     0.01  2807828     0.00     0.00  std::_Bit_iterator::operator++()
  2.94      0.25     0.01   146917     0.00     0.00  std::_Bit_iterator_base::_M_incr(int)
  2.94      0.26     0.01   121706     0.00     0.00  std::__miter_base<unsigned long*, false>::__b(unsigned long*)
  2.94      0.27     0.01    46008     0.00     0.00  std::_Bvector_base<std::allocator<bool> >::~_Bvector_base()
  2.94      0.28     0.01    22596     0.00     0.00  std::_Bit_iterator std::__copy_move<false, false, std::random_access_iterator_tag>::__copy_m<std::_Bit_iterator, std::_Bit_iterator>(std::_Bit_iterator, std::_Bit_iterator, std::_Bit_iterator)
  2.94      0.29     0.01     4525     0.00     0.05  integer::operator+(integer)
  2.94      0.30     0.01     1382     0.01     0.01  void std::_Destroy<unsigned int*, unsigned int>(unsigned int*, unsigned int*, std::allocator<unsigned int>&)
  2.94      0.31     0.01                             std::string::size() const
  2.94      0.32     0.01                             std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()
  2.94      0.33     0.01                             std::locale::locale()
  2.94      0.34     0.01                             __dynamic_cast

is that a good sign, since it means that the rest of my functions are pretty efficeint, or that accessing values from a vector< bool > is really slow?

im compiling with gcc -std=c++0x

like image 798
calccrypto Avatar asked Aug 05 '26 08:08

calccrypto


2 Answers

vector<bool> does not store bools. It's basically a bitfield. You're paying for the bit twiddling it takes to modify a single value.

If runtime performance is a concern, consider vector<char> or deque<bool> instead.

like image 170
Billy ONeal Avatar answered Aug 06 '26 21:08

Billy ONeal


since it means that the rest of my functions are pretty efficeint, or that accessing values from a vector is really slow?

As 'slow' and 'efficient' are relative values, this is essentially a senseless distinction. The most objective way to interpret the report is:

As std::vector' operation eat up the most significant amount of time, this should be the point to start with to make the code even faster.

Note that std::vector<bool> is generally a bit slower than a std::vector<int> because it does not store real bools but rather a set of bitmasks (i.e. ideally it needs only one bit per entry). This saves space, but is slower. If you need it to be faster, try using std::vector<int> (or char, .., depending on your needs) instead.

I would suspect that std::vector<bool> may suffer greatly from debug builds, so try some optimization flags if you didn't do that already (you always should for profiling).

like image 29
Alexander Gessler Avatar answered Aug 06 '26 22:08

Alexander Gessler