STL bitset class slow..

Discussion in 'C++' started by crea, Mar 4, 2011.

  1. crea

    crea Guest

    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;
    }
    crea, Mar 4, 2011
    #1
    1. Advertising

  2. crea

    Miles Bader Guest

    "crea" <> 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.'
    Miles Bader, Mar 4, 2011
    #2
    1. Advertising

  3. crea

    Goran Guest

    On Mar 4, 8:41 am, "crea" <> 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.
    Goran, Mar 4, 2011
    #3
  4. crea

    crea Guest

    Miles Bader wrote:
    > "crea" <> 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.
    crea, Mar 4, 2011
    #4
  5. crea

    red floyd Guest

    On Mar 4, 3:39 am, "crea" <> wrote:
    > Miles Bader wrote:
    > > "crea" <> 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.
    red floyd, Mar 4, 2011
    #5
  6. crea

    crea Guest

    red floyd wrote:
    > On Mar 4, 3:39 am, "crea" <> wrote:
    >> Miles Bader wrote:
    >>> "crea" <> 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...
    crea, Mar 4, 2011
    #6
  7. crea

    crea Guest

    crea wrote:
    > red floyd wrote:
    >> On Mar 4, 3:39 am, "crea" <> wrote:
    >>> Miles Bader wrote:
    >>>> "crea" <> 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.
    crea, Mar 4, 2011
    #7
  8. crea

    crea Guest

    red floyd wrote:
    > On Mar 4, 3:39 am, "crea" <> wrote:
    >> Miles Bader wrote:
    >>> "crea" <> 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.
    crea, Mar 4, 2011
    #8
  9. crea

    crea Guest

    red floyd wrote:
    > On Mar 4, 3:39 am, "crea" <> wrote:
    >> Miles Bader wrote:
    >>> "crea" <> 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
    crea, Mar 4, 2011
    #9
  10. 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
    Marcel Müller, Mar 4, 2011
    #10
  11. crea

    crea Guest

    Marcel Müller wrote:
    > 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.)


    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?
    crea, Mar 4, 2011
    #11
  12. crea

    crea Guest

    Marcel Müller wrote:
    > 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.


    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.
    > (An advantage, in my opinion.)
    >
    >
    > Marcel
    crea, Mar 4, 2011
    #12
  13. crea

    Dombo Guest

    Op 04-Mar-11 18:46, crea schreef:
    > crea wrote:
    >> red floyd wrote:
    >>> On Mar 4, 3:39 am, "crea"<> wrote:
    >>>> Miles Bader wrote:
    >>>>> "crea"<> 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.


    Only the Express editions don't come with MFC. The other editions of
    Visual Studio do include MFC (and ATL).
    Dombo, Mar 4, 2011
    #13
  14. crea

    crea Guest

    Marcel Müller wrote:
    > 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.)


    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 :).
    crea, Mar 4, 2011
    #14
  15. 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
    Marcel Müller, Mar 4, 2011
    #15
  16. crea

    Jorgen Grahn Guest

    On Fri, 2011-03-04, crea wrote:
    > Marcel Müller wrote:
    >> 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.)

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

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
    Jorgen Grahn, Mar 4, 2011
    #16
  17. crea

    RaZiel Guest

    On 04.03.2011 18:58, crea wrote:
    > red floyd wrote:
    >> On Mar 4, 3:39 am, "crea"<> wrote:
    >>> Miles Bader wrote:
    >>>> "crea"<> 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
    >
    >


    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
    RaZiel, Mar 5, 2011
    #17
  18. crea

    crea Guest

    RaZiel wrote:
    > On 04.03.2011 18:58, crea wrote:
    >>

    >
    > 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?
    crea, Mar 5, 2011
    #18
  19. crea

    crea Guest

    crea wrote:
    > RaZiel wrote:
    >> On 04.03.2011 18:58, crea wrote:
    >>>

    >>
    >> 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?


    Well its a nice secondary IDE at least... just hoping to get first windows
    program compiled... only DOS window appearing...
    crea, Mar 5, 2011
    #19
  20. crea

    crea Guest

    crea wrote:
    >
    > 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.
    crea, Mar 5, 2011
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. shaun roe

    Inheriting from STL bitset

    shaun roe, Nov 5, 2004, in forum: C++
    Replies:
    29
    Views:
    1,312
    Ivan Vecerina
    Nov 8, 2004
  2. Erick Crouse

    BitSet Class Implementation

    Erick Crouse, Nov 16, 2006, in forum: Java
    Replies:
    4
    Views:
    479
    Daniel Pitts
    Nov 20, 2006
  3. Sarath
    Replies:
    4
    Views:
    437
  4. arnuld
    Replies:
    2
    Views:
    443
    Ian Collins
    Jul 20, 2007
  5. Ninds
    Replies:
    14
    Views:
    708
    W Karas
    Dec 3, 2012
Loading...

Share This Page