vector 访问。
我使用gprof剖析了我的代码,并从报告中了解到,即使不是前20个左右,大多数(如果不是全部)都与向量有关
中访问值确实很慢?
我用gcc -std = c ++ 0x编译
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
这是个好兆头吗,因为这意味着我的其余函数都非常有效,或者从vector 没有找到相关结果
已邀请:
5 个回复
广躺椽
不存储
。它基本上是一个位域。您需要为修改单个值花些钱。 如果需要考虑运行时性能,请考虑使用
或
。
弛保矮瘦敖
(或
,..,取决于您的需要)。 我怀疑ѭ5可能会因调试版本而遭受很大的损失,因此,如果您还没有这样做,请尝试一些优化标志(您始终应该进行概要分析)。
凄挡
实际上是模板专门化,其中每个
值都存储为一个位。但是,不可能像使用
或仅使用\“ normal \”
一样直接使用单个位。因此,
中使用的算法与\“ normal \”
非常不同,并且为了尽可能多地保留
接口,它可能会返回代理对象,当您调用
之类的函数时,该对象会操纵这些位。这可能会对您的gprof报告中的结果有所贡献,具体取决于编译器的配置方式和相关代码。
耐钨徒
阅读问题的更新后进行编辑: 强制更改:
(这是两个错字和优化标志,重新配置!);模板类大量使用内联,这反过来又带来了数不胜数的其他优化。加速的顺序很容易达到10倍
森含械