STL bitset class slow..

C

crea

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;
}
 
M

Miles Bader

crea said:
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]"
 
G

Goran

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.
 
C

crea

Miles said:
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.
 
R

red floyd

Miles said:
[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.
 
C

crea

red said:
Miles said:
[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...
 
C

crea

crea said:
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.
 
C

crea

red said:
Miles said:
[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.
 
C

crea

red said:
Miles said:
[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
 
M

Marcel Müller

Hi,
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
 
C

crea

Marcel said:
Hi,


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.)

Ok, thanks... I will check that. I ll let you know. Interesting stuff...

Professionally speaking, do you think it is a good idea to learn these after
using VC since 1996? ITs free, so is it professional?? Is it as good as
Visual Studio?
 
C

crea

Marcel said:
Hi,


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.

The only problem would be the learnig curve. Am getting a bit old (41) so
not sure if its worth to put efford to learn this new thing... or maybe just
buy latest Visual C++. Have to think about this...

I like the idea its free, because then dont need to buy new VC versions
every second year.

Of course, gcc will not support MFC, because of license issues.
 
D

Dombo

Op 04-Mar-11 18:46, crea schreef:
crea said:
red said:
Miles Bader wrote:
[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.

Only the Express editions don't come with MFC. The other editions of
Visual Studio do include MFC (and ATL).
 
C

crea

Marcel said:
Hi,


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.)

Do they have a ready installation package including everything there? I will
try to find it myself as well ... :). But if you know, pls answer yes :).
 
M

Marcel Müller

crea wrote:
[CDT]
Do they have a ready installation package including everything there? I will
try to find it myself as well ... :). But if you know, pls answer yes :).

Don't know about a single package under Windows. (The Linux package
managers don't care about such details.)
In the worst case you must install Eclispe first and the CDT-Upgrade
matching your Eclipse release (most likely Helios) second.

Look at http://www.eclipse.org/cdt/


Marcel
 
J

Jorgen Grahn

Ok, thanks... I will check that. I ll let you know. Interesting stuff...

Professionally speaking, do you think it is a good idea to learn these after
using VC since 1996? ITs free, so is it professional??

In my experience (on Unix, not Windows) it's the /free/ development
tools which are professional, not the proprietary ones.

/Jorgen
 
R

RaZiel

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

If you'd go for wxWidgets and looking for a new C++ IDE for Windows, I'd
recommend CodeLite. It has a single installer with both GCC(MinGW) and
wxWidgets included. It feels natural even if you have used VS for a long
time. You can most likely still go for MFC using CodeLite by the way.

- RaZ
 
C

crea

RaZiel said:
If you'd go for wxWidgets and looking for a new C++ IDE for Windows,
I'd recommend CodeLite. It has a single installer with both
GCC(MinGW) and wxWidgets included. It feels natural even if you have
used VS for a long time. You can most likely still go for MFC using
CodeLite by the way.

Ok, I actually just installed that one...CodeLite because it was so easy.
Just to test. But I am not able to get windows project showing a windows. I
always just get a black dos window. Why is this?

I go to new project/GUI/.. and tried all wxWidgets project. But when I
compile them I just get a dos window not normal window. Is something still
missing from my computer?
 
C

crea

crea said:
Ok, I actually just installed that one...CodeLite because it was so
easy. Just to test. But I am not able to get windows project showing
a windows. I always just get a black dos window. Why is this?

I go to new project/GUI/.. and tried all wxWidgets project. But when I
compile them I just get a dos window not normal window. Is something
still missing from my computer?

Well its a nice secondary IDE at least... just hoping to get first windows
program compiled... only DOS window appearing...
 
C

crea

crea said:
Well its a nice secondary IDE at least... just hoping to get first
windows program compiled... only DOS window appearing...

Strange, I try to follow the "
Plugins

--------------------------------------------------------------------------------

a.. Creating a wxWidgets GUI App
"

instructions, but I dont get windows.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,783
Latest member
RickeyDort

Latest Threads

Top