473,387 Members | 3,781 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

The efficience of the bitset?

Now, i define the following variables:
bitset<32a, b;
int c, d;

and I have give some value to a, b, c, d, and if i compare (a == b),
is it like the integer compare like (c == d), or
it just do a loop 0 to 31 to compare if (a[i] == b[i]) and what is the
efficience compared with (c == d), if slow? is it
O(n) or just a little slower than O(1)?
Aug 25 '08 #1
4 5802
remlostime wrote:
Now, i define the following variables:
bitset<32a, b;
int c, d;

and I have give some value to a, b, c, d, and if i compare (a == b),
is it like the integer compare like (c == d), or
it just do a loop 0 to 31 to compare if (a[i] == b[i]) and what is the
efficience compared with (c == d), if slow? is it
O(n) or just a little slower than O(1)?
Not surprisingly the C++ standard doesn't specify how bitset is
internally implemented. However, most implementations (or, more
precisely, at least the gcc implementation) are very efficient and will
perform things like comparisons and bit counting as fast as they can.

(The bit counting of the gcc bitset is actually incredibly fast. Even
with really large bitsets it takes, in my computer, an average of 1
clock cycle per bit to count all the set bits. That's fast.)
Aug 25 '08 #2
Juha Nieminen wrote:
(The bit counting of the gcc bitset is actually incredibly fast. Even
with really large bitsets it takes, in my computer, an average of 1
clock cycle per bit to count all the set bits. That's fast.)
In 2003, libstdc++ switched from the table-based SGI implementation to
using builtins. I'd be curious to know how does dynamic_bitset<
unsigned long >::count() compares, on your machine; I'd expect it to
be close to the old libstdc++ (at least if you have CHAR_BIT == 8 and
no padding bits in unsigned longs :-)).

--
Gennaro Prota | name.surname yahoo.com
Breeze C++ (preview): <https://sourceforge.net/projects/breeze/>
Do you need expertise in C++? I'm available.
Aug 25 '08 #3
Gennaro Prota wrote:
In 2003, libstdc++ switched from the table-based SGI implementation to
using builtins. I'd be curious to know how does dynamic_bitset< unsigned
long >::count() compares, on your machine; I'd expect it to be close to
the old libstdc++ (at least if you have CHAR_BIT == 8 and no padding
bits in unsigned longs :-)).
My computer is a 3.4GHz Pentium4, and I'm using gcc 4.1.2 on a linux
system. I created a bitset with 2100000000 elements, with approximately
10% of the bits set (more precisely, discarding all the even numbers,
all the primes between 2 and 4200000000 are set as a set bit; the total
amount of set bits is 198996103).
I compiled the program with "-O3 -march=pentium4".

std::bitset::count() took approximately 590 ms.
boost::dynamic_bitset::count() took approximately 250 ms.

The latter seems to be significantly faster.

If my calculations are correct, this means that the former uses in
average approximately 0.95 clock cycles per bit, while the latter uses
in average only 0.4 clock cycles per bit.

That's *fast* bit counting...
Aug 26 '08 #4
Juha Nieminen wrote:
Gennaro Prota wrote:
>In 2003, libstdc++ switched from the table-based SGI implementation to
using builtins. I'd be curious to know how does dynamic_bitset< unsigned
long >::count() compares, on your machine; I'd expect it to be close to
the old libstdc++ (at least if you have CHAR_BIT == 8 and no padding
bits in unsigned longs :-)).

My computer is a 3.4GHz Pentium4, and I'm using gcc 4.1.2 on a linux
system. I created a bitset with 2100000000 elements, with approximately
10% of the bits set (more precisely, discarding all the even numbers,
all the primes between 2 and 4200000000 are set as a set bit; the total
amount of set bits is 198996103).
I compiled the program with "-O3 -march=pentium4".

std::bitset::count() took approximately 590 ms.
boost::dynamic_bitset::count() took approximately 250 ms.

The latter seems to be significantly faster.

If my calculations are correct, this means that the former uses in
average approximately 0.95 clock cycles per bit, while the latter uses
in average only 0.4 clock cycles per bit.

That's *fast* bit counting...
And that's nice to hear for me :-) Re-implementing count() was the
first thing I did on dynamic_bitset. If that doesn't bother you, you
might try another benchmark with find_first/find_next; in this case
libstdc++ and dynamic_bitset use completely different techniques. And
I have to say that, regardless of performance, I was quite happy of
how I implemented them without having built-ins at disposal (and
without a lookup table, either).

--
Gennaro Prota | name.surname yahoo.com
Breeze C++ (preview): <https://sourceforge.net/projects/breeze/>
Do you need expertise in C++? I'm available.
Aug 26 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Hunter Hou | last post by:
Hello, I have this very simple program, but it can't be compiled. What's wrong here? thanks, hunter #include <iostream>
2
by: shaun roe | last post by:
As a follow up to my question about STL and bitset<64> I'd like to share a quirk (bug?) about unsigned long long support and the bitset. I'm using gcc 3.2 on linux or gcc 3.3 on mac, the answer is...
5
by: SpOiLeR | last post by:
Hi. q1: Is std::bitset<N> part of standard or it's compiler extension? q2: Is std::bitset::to_string() part of standard? q3: My documentation say this about std::bitset::to_string(): ...
5
by: Rich S. | last post by:
Hi, Is the code below the best way to have the less than function for an 80 bit bitset or is there something faster / better? When I populate this map with millions (... and millions) of sets...
14
by: Haro Panosyan | last post by:
How to construct a bitset from string? For example: std::bitset<16> b16("1011011110001011"); Is this possible? Thanks in advance. -haro
3
by: shaun | last post by:
I have a function for returning the value of a bit field in a number: template<typename T> T bitfield(const T num, const unsigned int bitStart, const unsigned int bitEnd){ T mask,...
4
by: Sarath | last post by:
>From the documentation of MSDN, it is saying that bitset is not a STL container Unlike the similar vector<boolClass, the bitset class does not have iterators and is not an Standard Template...
2
by: arnuld | last post by:
i am confused on some aspects of bitset class: /* C++ Primer 4/e * chapter 3 * * exercise 3.23 * */ #include <iostream>
5
by: swcamry | last post by:
class bitset::reference { friend class bitset; reference(); // no public constructor public: ~reference(); operator bool () const; //...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.