根据this answer,海报期望在查询单个位时,std::bitset
的大小为100k位比std::vector<bool>
快。怎么可能呢?
如果std::bitset
显然允许std::vector
之类的任意尺寸,那么它们的实施方式甚至会有显着差异吗?
答案 0 :(得分:17)
Visual Studio 2010上的测量表明,std::bitset
不通常比std::vector<bool>
更快。究竟是什么原因我不能说 - 只有bitset的实现与std :: vector完全特化有很大不同。
std :: bitset通过
将它的全部内容存储在对象中template<size_t _Bits>
class bitset .....
_Ty _Array[_Words + 1]; // the set of bits
};
数组,这使得大型bitset不适合放在堆栈上 - 这本身并不是性能参数。
vector<bool>
不会遇到堆栈问题,并且测试大小为1e6和1e7似乎在我的框中,在这里查询循环中的值实际上是向量的2倍。
好。我想通常的时间警告适用和YMMV,但这里是我使用的测试代码,任何人都应该自己尝试:
我的框上的输出是:
1
vector<bool> loop with a size of 10000000 and 10 iterations*n: 11187 ms
bitset<10000000> loop with 10 iterations*n: 22719 ms
101250010
Press any key to continue . . .
BitMap.cpp
#include "stdafx.h"
#include "BitMap.h"
using namespace std;
// Global var to prevent optimizer from messing things up
volatile size_t ext;
volatile clock_t t1;
volatile clock_t t2;
double delta1;
double delta2;
int main(int argc, _TCHAR* argv[])
{
ext = 1;
printf("%d\n", ext);
vb_t *const vec = new vb_t(bssz);
bs_t *const bits = new bs_t(); // must put large bitset on heap
const int iter = 10;
delta1=0;
delta2=0;
for(int o=0; o<5; ++o) {
t1 = clock();
for(int i=0; i!=5; ++i)
bs_loop(iter, *vec);
t2 = clock();
delta1 += t2-t1;
t1 = clock();
for(int i=0; i!=5; ++i)
bs_loop(iter, *bits);
t2 = clock();
delta2 += t2-t1;
}
delta1 /= CLOCKS_PER_SEC;
delta2 /= CLOCKS_PER_SEC;
delta1 *= 1000;
delta2 *= 1000;
cout << "vector<bool> loop with a size of " << bssz << " and " << iter << " iterations*n: " << delta1 << " ms\n";
cout << "bitset<" << bssz << "> loop with " << iter << " iterations*n: " << delta2 << " ms\n";
printf("%d\n", ext);
delete vec;
delete bits;
return 0;
}
BitMap.h
#pragma once
#include <vector>
#include <bitset>
extern volatile size_t ext;
const size_t bssz = size_t(1e7); // 1e7 ca 10m
using namespace std; // Test code, using here is OK.
typedef vector<bool> vb_t;
typedef bitset<bssz> bs_t;
template<class COLL>
void bs_loop(const int iterations, COLL const& v);
bs_loop.cpp
#include "stdafx.h"
#include "BitMap.h"
template<class COLL>
void bs_loop(const int iterations, COLL const& v)
{
ext = sizeof(COLL);
for(size_t i=0; i!=iterations; ++i) {
++ext;
for(size_t j=0, e=v.size(); j!=e; ++j) {
if(v[j]) {
--ext;
}
else {
++ext;
}
}
}
}
template
void bs_loop(const int iterations, vb_t const& v);
template
void bs_loop(const int iterations, bs_t const& v);
编译器命令行:
/Zi /nologo /W3 /WX- /O2 /Oi /Oy- /D "WIN32" /D "NDEBUG"
/D "_CONSOLE" /D "_UNICODE" /D "UNICODE" /Gm- /EHsc /GS /Gy
/fp:precise /Zc:wchar_t /Zc:forScope /Yu"StdAfx.h" /Fp"Release\BitMap.pch"
/Fa"Release\" /Fo"Release\" /Fd"Release\vc100.pdb" /Gd /analyze-
/errorReport:queue
注意/ O2和缺少 / GL(没有整个prg选项)。
答案 1 :(得分:6)
好吧,既然我就是这个问题的基础,here's where I got that idea from:
“......它包含bool并将它们作为单独的位(内部,比如,字符)存储在其内部表示中。这样做的一个结果是它不能仅从其运算符[]或其中返回正常的bool&amp;取消引用迭代器[2];相反,它必须使用类似bool的辅助“代理”类来玩游戏,但绝对不是bool。不幸的是,这也意味着访问vector<bool>
的速度较慢,因为我们必须处理代理而不是直接指针和引用。
...
结论:如果您更关心速度而不是尺寸,则不应使用std::vector<bool>
。相反,你应该使用std::vector<char>
之类的东西来破解这种优化,这是不幸的,但仍然是你能做到的最好的。“
或者,正如我所建议的,如果你知道你的设置最大尺寸,请使用std::bitset
。
答案 2 :(得分:2)
老实说,我觉得bitset最好在堆栈中使用而不是在堆上。 而且这两者并不相互冲突,因为优雅的解决方案可能是这样的:
vector< bitset<64> > v(100000) //or whatever...
相比之下,这可能是一个有趣的测试:
vector<unsigned char> v1(1000000) //8 bits to manage them manually
vector< bitset<8> > v2(1000000) //8 bits managed by bitset
此外,只是为了在这里添加答案并提醒编译器如何依赖性能,这里是一个简单的测试:
(但所有这些测试都有点棘手,可能只给我们一个粗略的总体思路进行DIRECT比较。分析项目到底是唯一能做到的。)
注意:
,大小为10 ^ 7:
我也包含了对象的构造函数和析构函数的开销时间。
这里是简单的测试代码:
#include <iostream>
#include <vector>
#include <bitset>
#include <time.h>
using namespace std;
#define SIZE1 1000000000 //10e9
//#define SIZE2 10000000 //10e7 VS2012 crash at runtime, g++ OK
#define SIZE2 1000000 //10e6
void test1()
{
register bool j;
clock_t t1,t2;
cout.precision(10);
t1=clock();
vector<bool> *v = new vector<bool>(SIZE1);
for(register long int i=0; i<SIZE1;i++)
(*v)[i] = i%2 == 0? true :false;
for(register long int i=0; i<SIZE1;i++)
j=(*v)[i];
delete v;
t2=clock();
cout << "vector speed = " << (t2-t1) / (float) CLOCKS_PER_SEC << " (" << t2 << "," << t1 << ")" << endl;
t1=clock();
bitset<SIZE1> *b = new bitset<SIZE1>();
for(register long int i=0; i<SIZE1;i++)
(*b)[i] = i%2 == 0? true :false;
for(register long int i=0; i<SIZE1;i++)
j=(*b)[i];
delete b;
t2=clock();
cout << "bitset speed = " << (t2-t1) / (float) CLOCKS_PER_SEC << " (" << t2 << "," << t1 << ")" << endl;
}
void test2()
{
register bool j;
clock_t t1,t2;
cout.precision(10);
t1=clock();
vector<bool> v(SIZE2);
for(register int k=0; k<SIZE1/SIZE2; k++)
for(register long int i=0; i<SIZE2;i++)
(v)[i] = i%2 == 0? true :false;
for(register int k=0; k<SIZE1/SIZE2; k++)
for(register long int i=0; i<SIZE2;i++)
j=(v)[i];
t2=clock();
cout << "vector speed = " << (t2-t1) / (float) CLOCKS_PER_SEC << " (" << t2 << "," << t1 << ")" << endl;
cout << "v[1], v[2] " << (int) v[1] << ", "<< (int)v[2] << endl;
t1=clock();
bitset<SIZE2> b;
for(register int k=0; k<SIZE1/SIZE2; k++)
for(register long int i=0; i<SIZE2;i++)
(b)[i] = i%2 == 0? true :false;
for(register int k=0; k<SIZE1/SIZE2; k++)
for(register long int i=0; i<SIZE2;i++)
j=(b)[i];
t2=clock();
cout << "bitset speed = " << (t2-t1) / (float) CLOCKS_PER_SEC << " (" << t2 << "," << t1 << ")" << endl;
cout << "b[1], b[2] " << (int) b[1] << ", "<< (int)b[2] << endl;
}
int main(int argc, char* argv[])
{
test1();
test2();
return 0;
}
VS2012输出:
vector speed = 3.105000019 (3105,0)
bitset speed = 10.44400024 (13551,3107)
vector speed = 3.987999916 (17542,13554)
v[1], v[2] 0, 1
bitset speed = 9.772999763 (27318,17545)
b[1], b[2] 0, 1
mingw / g ++输出-O2:
vector speed = 1.519 (1520,1)
bitset speed = 1.647 (3168,1521)
vector speed = 1.383999944 (4554,3170)
v[1], v[2] 0, 1
bitset speed = 1.610000014 (6166,4556)
b[1], b[2] 0, 1
mingw / g ++输出-O2 -std = c ++ 11:
vector speed = 1.528 (1529,1)
bitset speed = 1.685 (3215,1530)
vector speed = 1.409999967 (4626,3216)
v[1], v[2] 0, 1
bitset speed = 1.763000011 (6392,4629)
b[1], b[2] 0, 1
g ++ 4.8.2输出-O2:
vector speed = 1.561391 (1564139,2748)
bitset speed = 1.681818 (3246051,1564233)
vector speed = 1.487877011 (4733975,3246098)
v[1], v[2] 0, 1
bitset speed = 1.685297012 (6419328,4734031)
b[1], b[2] 0, 1
g ++ 4.8.2输出-O2 -std = c ++ 11:
vector speed = 1.561391 (1564139,2748)
bitset speed = 1.681818 (3246051,1564233)
vector speed = 1.487877011 (4733975,3246098)
v[1], v[2] 0, 1
bitset speed = 1.685297012 (6419328,4734031)
b[1], b[2] 0, 1
<强>结论:强>
作为一个粗略的想法,这些用例似乎更快。
我没有运行多个istances并对结果取平均值,但是或多或少的值始终是相同的。
关于VS的注释 :我认为它使用与gcc不同的内存管理机制,并且对于这些用例在生成的代码中似乎较慢。
答案 3 :(得分:1)
向量使用迭代器访问其元素,迭代器不能是bool *的简单typedef,这使得它比bitset慢,后者不提供迭代器。使其快速的另一个原因是它的大小是已知的编译时间,因此它不使用new进行分配,这比堆栈分配慢。只是随机的想法
答案 4 :(得分:1)
这是我从大约100K,1M和5M的bitset<>
和vector<bool>
访问/插入30亿个元素的不科学基准。编译器是64位Linux机器(Core i7)上的GCC 4.8.2:
使用优化(编译器标志:-O2 -std=c++11
):
[estan@pyret bitset_vs_vector]$ ./bitset_vs_vector
bitset<100000> (3 billion accesses/inserts): 132.424 ms
vector<bool>(100000) (3 billion accesses/inserts): 270.577 ms
bitset<1000000> (3 billion accesses/inserts): 67.752 ms
vector<bool>(1000000) (3 billion accesses/inserts): 268.193 ms
bitset<5000000> (3 billion accesses/inserts): 67.426 ms
vector<bool>(5000000) (3 billion accesses/inserts): 267.566 ms
没有优化(编译器标志:-std=c++11
):
[estan@pyret bitset_vs_vector]$ make
g++ -std=c++11 -o bitset_vs_vector *.cpp
[estan@pyret bitset_vs_vector]$ ./bitset_vs_vector
bitset<100000> (3 billion accesses/inserts): 1900.13 ms
vector<bool>(100000) (3 billion accesses/inserts): 1784.76 ms
bitset<1000000> (3 billion accesses/inserts): 1825.09 ms
vector<bool>(1000000) (3 billion accesses/inserts): 1768.03 ms
bitset<5000000> (3 billion accesses/inserts): 1846.73 ms
vector<bool>(5000000) (3 billion accesses/inserts): 1763.48 ms
所以看起来在这些条件下,当代码被优化时,bitset比vector更快,而vector实际上是以(非常)小的边缘出现的,而不是。
也就是说,如果你的代码是时间关键的,你应该自己执行基准测试,因为我怀疑这些数字是高度编译器/环境特定的。
基准代码:
#include <iostream>
#include <functional>
#include <bitset>
#include <vector>
#include <ctime>
// Performs N access/insert on container.
template<class T>
void access_and_insert(T &container, int N)
{
const std::size_t size = container.size();
for (int i = 0; i < N; ++i) {
bool v = container[i % size];
container[i % size] = true;
}
}
// Measure the time in milliseconds required to call f.
double measure(std::function<void (void)> f)
{
clock_t start = std::clock();
f();
return 1000.0 * (std::clock() - start)/CLOCKS_PER_SEC;
}
int main (void)
{
// Benchmark with 100K elements.
std::bitset<100000> bitset100K;
std::vector<bool> vector100K(100000);
std::cout << "bitset<100000> (3 billion accesses/inserts): ";
std::cout << measure([&]() { access_and_insert(bitset100K, 3E7); }) << " ms " << std::endl;
std::cout << "vector<bool>(100000) (3 billion accesses/inserts): ";
std::cout << measure([&]() { access_and_insert(vector100K, 3E7); }) << " ms" << std::endl;
std::cout << std::endl;
// Benchmark with 1M elements.
std::bitset<1000000> bitset1M;
std::vector<bool> vector1M(1000000);
std::cout << "bitset<1000000> (3 billion accesses/inserts): ";
std::cout << measure([&]() { access_and_insert(bitset1M, 3E7); }) << " ms " << std::endl;
std::cout << "vector<bool>(1000000) (3 billion accesses/inserts): ";
std::cout << measure([&]() { access_and_insert(vector1M, 3E7); }) << " ms" << std::endl;
std::cout << std::endl;
// Benchmark with 5M elements.
std::bitset<5000000> bitset5M;
std::vector<bool> vector5M(5000000);
std::cout << "bitset<5000000> (3 billion accesses/inserts): ";
std::cout << measure([&]() { access_and_insert(bitset5M, 3E7); }) << " ms " << std::endl;
std::cout << "vector<bool>(5000000) (3 billion accesses/inserts): ";
std::cout << measure([&]() { access_and_insert(vector5M, 3E7); }) << " ms" << std::endl;
return 0;
}
答案 5 :(得分:-2)
另请注意,vector<bool>
是矢量模板的特化,其实现方式与您想象的完全不同。