连接 boost::dynamic_bitset 或 std::bitset
连接 2 个位集的最佳方法是什么?
例如,我知道
boost::dynamic_bitset<> test1( std::string("1111") );
boost::dynamic_bitset<> test2( std::string("00") );
它们应该连接到第三个位集 test3 中,然后它保存
111100
解决方案应该使用 boost::dynamic_bitset。如果该解决方案适用于 std::bitset,那就太好了。连接位时应关注性能。
更新: 我比较了这两种方法(我和 Neil 的 stringmethod 以及 Messenger 的 shiftmethod),并且 stringmethod 更快(因子 10++)。代码在这里: http://pastebin.com/HfpfYfy8
我希望 Pastebin 可以发布长代码列表。如果有更好的方法请联系我。
what is the best way to concatenate 2 bitsets?
For example i've got
boost::dynamic_bitset<> test1( std::string("1111") );
boost::dynamic_bitset<> test2( std::string("00") );
they should be concatenated into a thrid Bitset test3 which then holds
111100
Solutions should use boost::dynamic_bitset. If the solution works with std::bitset, it would be nice too. There should be a focus on performance when concatenating the bits.
UPDATE:
I've compared both methods (stringmethod from me and Neil and shiftmethod from messenger) and the stringmethod was a lot faster (factor 10++). Code here:
http://pastebin.com/HfpfYfy8
I hope Pastebin is ok for posting long code-listings. If there is a better way please contact me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
对于标准位集,类似于:
For the standard bitset, something like:
我已经测试了几种解决方案,似乎:
这是我的测试代码:
这是我在计算机上使用 VS7.1 得到的输出:
您可以注意到我使用循环编写的函数产生了不同的结果。这是因为我当时写的是将第二个位集的 lsb 放在第一个位集的 msb 之后(lsb 在右侧)。使用字符串或“位运算符”功能,您只需切换调用参数即可。
I've tested several solutions and it seems that :
Here is my test code :
Here is the output I get with VS7.1 on my computer:
You can notice that the function I wrote using loops produce differents results. It's because I wrote then to put the lsb of the second bitset after the msb of the first one (lsb on the right). With the string or "bit operator" function you just have to switch the calling parameters.
我运行了一个测试,比较以下两种方法:
... 其中 randomUlongs 在每次运行期间都是恒定的(标头中的大数组)以避免任何污染结果。我用以下方法计时:
在 Linux (x86_i686) 下,使用
gcc 4.4.6
优化级别 3:字符串连接速度最快,为 2 倍。在 Solaris (sparc) 下,使用
gcc 3.4 .3
和Sun Studio C++ 5.12 (2011/11/16)
,两者的优化级别都是 3:非字符串方法的速度快了 10 倍。我想您会找到“最快”的解决方案高度依赖于编译器,尽管我认为平台也可以发挥重要作用。
I ran a test comparing the following two approaches:
... where
randomUlongs
was constant during each run (a large array in a header) to avoid anything contaminating results. I timed it with:Under Linux (x86_i686) with
gcc 4.4.6
at optimization level 3: the string concatenation was fastest, by a factor of 2.Under Solaris (sparc) with
gcc 3.4.3
andSun Studio C++ 5.12 (2011/11/16)
, both with optimization level 3: the non-string approach was fastest by a factor of 10.I think you'll find the "fastest" solution is highly dependent on the compiler, though I suppose platform could play a significant role as well.
首先,我将自己添加一个可能的解决方案。以下代码使用了使用 std::string 构造位集并从位集生成 std::string 的可能性。
这可行,但必须有其他更高效的解决方案...
更新:
感谢 JC Leitão 的编辑建议
For getting started, i'll add a possible solution by myself. The following code uses the possibility to construct bitsets with std::string and to generate a std::string from a bitset.
This works, but there must be other, more performant solutions...
Update:
Thanks to J. C. Leitão for his edit-suggestion
这是一个解决方案。不确定是否可以编译。
Here is a stab at a solution. Not sure if it compiles.