Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > STL bitset class slow..

Reply
Thread Tools

STL bitset class slow..

 
 
crea
Guest
Posts: n/a
 
      03-04-2011
How come STL library class is slower than normal C-code? I read from
somewhere that STL classes (vectors) are optmized for performance, so best
to use them. But I did a test and bitset failed.

I made a C-code and then the same with bitset class. C-code did the job in 2
seconds but bitset in 14 seconds. Quite a difference.

Really true that bitset -class is slow? I was thinking of using it , but I
need it to be very fast. If its this slow I cannot use it...

Code:
Test 1; C-version:

int n = 10001;
for(int i=0; i<1000000000;i++)
{
n |= i; // bit-or operation
}

Test 2; STL-version:

bitset<32> bi;
bitset<32> bi2;
for(int i=0; i<1000000000;i++)
{
bi |= bi2;
}




 
Reply With Quote
 
 
 
 
Miles Bader
Guest
Posts: n/a
 
      03-04-2011
"crea" <(E-Mail Removed)> writes:
> How come STL library class is slower than normal C-code? I read from
> somewhere that STL classes (vectors) are optmized for performance, so
> best to use them. But I did a test and bitset failed.


You didn't specify your compiler, environment, or options used, so it's
hard to say.

I tried compiling the following program using g++-4.6, and it produced
identical code for "int" and "bitset<32>":

#include <bitset>

using namespace std;

int test_int ()
{
int n = 10001;
for(int i=0; i<1000000000;i++)
n |= i; // bit-or operation
return n;
}

bitset<32> test_bitset ()
{
bitset<32> bi (10001);
for(int i=0; i<1000000000;i++)
bi |= bitset<32> (i);
return bi;
}

Here's the result:

$ g++-4.6 -o - -S -march=native -O2 x.cc |cleanasm

.globl test_int()
test_int():
xorl %edx, %edx
movl $10001, %eax
.L2:
orl %edx, %eax
addl $1, %edx
cmpl $1000000000, %edx
jne .L2
rep
ret

.globl test_bitset()
test_bitset():
xorl %edx, %edx
movl $10001, %eax
.L6:
orq %rdx, %rax
addq $1, %rdx
cmpq $1000000000, %rdx
jne .L6
rep
ret

.ident "GCC: (Debian 4.6-20110216-1) 4.6.0 20110216 (experimental) [trunk revision 170225]"



--
`There are more things in heaven and earth, Horatio,
Than are dreamt of in your philosophy.'
 
Reply With Quote
 
 
 
 
Goran
Guest
Posts: n/a
 
      03-04-2011
On Mar 4, 8:41*am, "crea" <(E-Mail Removed)> wrote:
> How come STL library class is slower than normal C-code? I read from
> somewhere that STL classes (vectors) are optmized for performance, so best
> to use them. But I did a test and bitset failed.
>
> I made a C-code and then the same with bitset class. C-code did the job in 2
> seconds but bitset in 14 seconds. Quite a difference.
>
> Really true that bitset -class is slow? I was thinking of using it , but I
> need it to be very fast. If its this slow I cannot use it...
>
> Code:
> Test 1; C-version:
>
> int n = 10001;
> * for(int i=0; i<1000000000;i++)
> *{
> *n |= i; // bit-or operation
> *}
>
> Test 2; STL-version:
>
> bitset<32> bi;
> bitset<32> bi2;
> *for(int i=0; i<1000000000;i++)
> *{
> * bi |= bi2;
> *}


I think that you speed-tested unoptimized code. Don't, that's
meaningless.

On VC 2008 over here, optimizer removes the "int" loop completely. It
also removes bi |= bi2 completely (but, strangely, not the bitset
loop). If I add a side-effect, effectively tricking the compiler to
generate "int" loop, it runs about twice as slow as "bitset" loop (who
is effectively empty, but present).

Here's my code:

static const unsigned int giga = 1024*1024*1024;
DWORD dw = GetTickCount();
int n = 10001;
for(int i=0; i<giga;i++)
n |= i; // bit-or operation
cout << GetTickCount()-dw << endl;

// Size-effect necessary to trick this particular compiler
// to emit code for the loop above. Without it, loop iz optimized
out.
cout << n << endl;

dw = GetTickCount();
bitset<32> bi;
bitset<32> bi2;
for(int i=0; i<giga;i++)
bi |= bi2;

cout << GetTickCount()-dw << endl;


And here's what relevant parts compile to on my compiler (compiled
with /Ox):

for(int i=0; i<giga;i++)
00F41035 xor eax,eax
n |= i; // bit-or operation
00F41037 or esi,eax // n is in esi, i in eax
00F41039 inc eax
00F4103A cmp eax,40000000h // giga here
00F4103F jb wmain+37h (0F41037h) // loop end condition

for(int i=0; i<giga;i++)
00F41082 mov ecx,40000000h // i is ecx, and strangely, counts
backwards. Ask MS why
00F41087 sub ecx,1 // count backwards
00F4108A jne wmain+87h (0F41087h) // loop end condition
bi |= bi2; // optimized out, no code for this - at all

Goran.
 
Reply With Quote
 
crea
Guest
Posts: n/a
 
      03-04-2011
Miles Bader wrote:
> "crea" <(E-Mail Removed)> writes:
>> How come STL library class is slower than normal C-code? I read from
>> somewhere that STL classes (vectors) are optmized for performance, so
>> best to use them. But I did a test and bitset failed.

>
> You didn't specify your compiler, environment, or options used, so
> it's
> hard to say.


True, I forgot the optimization issue.... I use VC6++. I need to do the same
without optimization.


 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      03-04-2011
On Mar 4, 3:39*am, "crea" <(E-Mail Removed)> wrote:
> Miles Bader wrote:
> > "crea" <(E-Mail Removed)> writes:

[redacted]
> True, I forgot the optimization issue.... I use VC6++. I need to do the same
> without optimization.


VC6 is horrendously out of date, and predates even the 1998 standard
(much less
the 2003 update).

Get a *REAL* compiler. GCC is available for Windows, and VS2010
Express is
is available for free.


 
Reply With Quote
 
crea
Guest
Posts: n/a
 
      03-04-2011
red floyd wrote:
> On Mar 4, 3:39 am, "crea" <(E-Mail Removed)> wrote:
>> Miles Bader wrote:
>>> "crea" <(E-Mail Removed)> writes:

> [redacted]
>> True, I forgot the optimization issue.... I use VC6++. I need to do
>> the same without optimization.

>
> VC6 is horrendously out of date, and predates even the 1998 standard
> (much less
> the 2003 update).
>
> Get a *REAL* compiler. GCC is available for Windows, and VS2010
> Express is
> is available for free.


The problem is, I think, they dont have proper graphical programming
possibilities, which VC6 has. I need graphics as well!! Express does not
have included...


 
Reply With Quote
 
crea
Guest
Posts: n/a
 
      03-04-2011
crea wrote:
> red floyd wrote:
>> On Mar 4, 3:39 am, "crea" <(E-Mail Removed)> wrote:
>>> Miles Bader wrote:
>>>> "crea" <(E-Mail Removed)> writes:

>> [redacted]
>>> True, I forgot the optimization issue.... I use VC6++. I need to do
>>> the same without optimization.

>>
>> VC6 is horrendously out of date, and predates even the 1998 standard
>> (much less
>> the 2003 update).
>>
>> Get a *REAL* compiler. GCC is available for Windows, and VS2010
>> Express is
>> is available for free.

>
> The problem is, I think, they dont have proper graphical programming
> possibilities, which VC6 has. I need graphics as well!! Express does
> not have included...


The problem is that I have learned to use MFC. So these dont have it... its
a bit difficult to change.


 
Reply With Quote
 
crea
Guest
Posts: n/a
 
      03-04-2011
red floyd wrote:
> On Mar 4, 3:39 am, "crea" <(E-Mail Removed)> wrote:
>> Miles Bader wrote:
>>> "crea" <(E-Mail Removed)> writes:

> [redacted]
>> True, I forgot the optimization issue.... I use VC6++. I need to do
>> the same without optimization.

>
> VC6 is horrendously out of date, and predates even the 1998 standard
> (much less
> the 2003 update).
>
> Get a *REAL* compiler. GCC is available for Windows, and VS2010
> Express is
> is available for free.


Ok, you got me thinking... let me check that GCC if it can do Win programs.
I doubt though... but lets see.


 
Reply With Quote
 
crea
Guest
Posts: n/a
 
      03-04-2011
red floyd wrote:
> On Mar 4, 3:39 am, "crea" <(E-Mail Removed)> wrote:
>> Miles Bader wrote:
>>> "crea" <(E-Mail Removed)> writes:

> [redacted]
>> True, I forgot the optimization issue.... I use VC6++. I need to do
>> the same without optimization.

>
> VC6 is horrendously out of date, and predates even the 1998 standard
> (much less
> the 2003 update).
>
> Get a *REAL* compiler. GCC is available for Windows, and VS2010
> Express is
> is available for free.


hmmm, GCC with wxwidgets might do the job... must check


 
Reply With Quote
 
Marcel Müller
Guest
Posts: n/a
 
      03-04-2011
Hi,

crea wrote:
>> Get a *REAL* compiler. GCC is available for Windows, and VS2010
>> Express is
>> is available for free.

>
> Ok, you got me thinking... let me check that GCC if it can do Win programs.
> I doubt though... but lets see.


if you think about gcc, you will most likely need an additional IDE. I
would recommend Eclipse CDT (free). It will outperform the VC6 IDE by far.
Of course, gcc will not support MFC, because of license issues. (An
advantage, in my opinion.)


Marcel
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bitset<32> and bitset<64> efficiency Ninds C++ 14 12-03-2012 11:02 PM
some questions about "bitset" class arnuld C++ 2 07-20-2007 08:06 AM
Is bitset class is not a STL container? Sarath C++ 4 05-31-2007 06:36 PM
BitSet Class Implementation Erick Crouse Java 4 11-20-2006 06:45 PM
Inheriting from STL bitset shaun roe C++ 29 11-08-2004 06:03 PM



Advertisments