我最近遇到了 bitset 模板,我真的很想在我当前的项目中使用它们。继续阅读,我看到std::bitset
模板的大小必须在编译时确定。许多人建议使用boost::dynamic_bitset
来减轻此要求。
为了比较两者,我决定对 、 和 方法进行set
速度flip
比较count
。
结果很奇怪......我想知道是否有人可以为我解释一下。
代码在帖子的末尾,但我会在这里解释我在做什么。我有一个std::bitset
对象(调用它bs
)和一个boost::dynamic_bitset
对象(调用它dynbs
)。每个都有n=1000000
位。对于上面的给定方法,n
依次调用每个位上的方法并重复此R=10000
时间。
使用该std::chrono
库,以下是每个以纳秒为单位的时间:
set
bitset: 267 nsecs
dyn bitset: 18603174546 nsecs
flip
bitset: 73 nsecs
dyn bitset: 18842352867 nsecs
count
bitset: 77 nsecs
dyn bitset: 51 nsecs
和boost::dynamic_bitset
似乎要慢set
得多flip
。
更有趣的是,如果reset
在运行这些测试之前对两个对象调用该方法,那么时间是可比较的。他们来了:
set
bitset: 19397779399 nsecs
dyn bitset: 18472863864 nsecs
flip
bitset: 18599248629 nsecs
dyn bitset: 18376267939 nsecs
count
bitset: 68 nsecs
dyn bitset: 61 nsecs
现在,两个容器都声称将所有位初始化为0
,因此调用reset
不应更改任何位。转储none
之前和之后的输出reset
确实证实了这一点。
所以毕竟,我有两个问题:
1)为什么比调用时和时boost::dynamic_bitset
慢得多?std::bitset
set
flip
2)为什么调用reset
对速度有巨大的负面影响std::bitset
?
这是我的代码:
#include <iostream>
#include <iomanip>
#include <bitset>
#include <boost/dynamic_bitset.hpp>
#include <vector>
#include <chrono>
#include <ctime>
using namespace std;
using namespace chrono;
using namespace boost;
int main(){
const unsigned int n=1000000;
bitset< n > bs;
dynamic_bitset< > dynbs(n);
// bs.reset();
// dynbs.reset();
unsigned int i,r,R=10000;
high_resolution_clock::time_point tick,tock;
////////////////////////////////////////////////////////////
// Method: set
std::cout << "set" << std::endl;
tick=high_resolution_clock::now();
for(r=0; r<R; r++)
for(i=0; i<n; i++)
bs.set(i);
tock=high_resolution_clock::now();
cout << setw(16) << "bitset: "
<< setw(16) << duration_cast<nanoseconds>(tock-tick).count() << " nsecs"
<< std::endl;
tick=high_resolution_clock::now();
for(r=0; r<R; r++)
for(i=0; i<n; i++)
dynbs.set(i);
tock=high_resolution_clock::now();
cout << setw(16) << "dyn bitset: "
<< setw(16) << duration_cast<nanoseconds>(tock-tick).count() << " nsecs"
<< std::endl << std::endl;
////////////////////////////////////////////////////////////
// Method: flip
std::cout << "flip" << std::endl;
tick=high_resolution_clock::now();
for(r=0; r<R; r++)
for(i=0; i<n; i++)
bs.flip(i);
tock=high_resolution_clock::now();
cout << setw(16) << "bitset: "
<< setw(16) << duration_cast<nanoseconds>(tock-tick).count() << " nsecs"
<< std::endl;
tick=high_resolution_clock::now();
for(r=0; r<R; r++)
for(i=0; i<n; i++)
dynbs.flip(i);
tock=high_resolution_clock::now();
cout << setw(16) << "dyn bitset: "
<< setw(16) << duration_cast<nanoseconds>(tock-tick).count() << " nsecs"
<< std::endl << std::endl;
////////////////////////////////////////////////////////////
// Method: count
std::cout << "count" << std::endl;
tick=high_resolution_clock::now();
for(r=0; r<R; r++)
for(i=0; i<n; i++)
bs.count();
tock=high_resolution_clock::now();
cout << setw(16) << "bitset: "
<< setw(16) << duration_cast<nanoseconds>(tock-tick).count() << " nsecs"
<< std::endl;
tick=high_resolution_clock::now();
for(r=0; r<R; r++)
for(i=0; i<n; i++)
dynbs.count();
tock=high_resolution_clock::now();
cout << setw(16) << "dyn bitset: "
<< setw(16) << duration_cast<nanoseconds>(tock-tick).count() << " nsecs"
<< std::endl;
return 0;
}
我用它编译了
g++ -O3 -std=c++11 bitset.cpp -o bitset
bitset.cpp
上面插入的代码在哪里。