vector.resize() taking a short time on AMD, a long time on Intel

Discussion in 'C++' started by Jim Langston, May 31, 2006.

  1. Jim Langston

    Jim Langston Guest

    I really am not sure if this question belongs in this newsgroup, but not
    sure where else to ask it.

    There is someone working on a game that I tested, and it was taking >30
    seconds to load. He stated that everyone else was taking 2 or 3 seconds.
    Then he found one other person taking >30 seconds, and it turns out the
    common denominator was both of us have Intel chips (Celeron) where the other
    people have AMD.

    I had him send me his code and he was doing a lot of .resize() in nested
    vectors.

    surfmap->IDR[surfindex].resize(bpgmap->width);for(y=0;ywidth;++y) {
    surfmap->IDR[surfindex][y].resize(bpgmap->height+2); }
    surfmap->IDG[surfindex].resize(bpgmap->width);for(y=0;ywidth;++y) {
    surfmap->IDG[surfindex][y].resize(bpgmap->height+2); }
    surfmap->IDB[surfindex].resize(bpgmap->width);for(y=0;ywidth;++y) {
    surfmap->IDB[surfindex][y].resize(bpgmap->height+2); }
    surfmap->IDA[surfindex].resize(bpgmap->width);for(y=0;ywidth;++y) {
    surfmap->IDA[surfindex][y].resize(bpgmap->height+2); }

    I looked at this and had him display clock() times before and after, and
    sometimes this block of code would take >200ms on our Intel chips, but
    ususually ~30ms on AMD.

    I wrote a class for him that allocated the block of memory with one call to
    new and gave him accessors which he replaced this with and now clock is
    reporting 0 ms on AMD (haven't tested it on Intel yet).

    My question is, why would the .resize() take so much longer on Intel than
    AMD? Is it something specific to the Celeron maybe?

    Before I rewrote it as a class for him I had considered things such as
    ..clear() .reserve() but that didn't immediately update the .size() for the
    vector, and I think the class is the best way to go after all.
    Jim Langston, May 31, 2006
    #1
    1. Advertising

  2. Jim Langston

    Ian Collins Guest

    Jim Langston wrote:
    > I really am not sure if this question belongs in this newsgroup, but not
    > sure where else to ask it.
    >
    > There is someone working on a game that I tested, and it was taking >30
    > seconds to load. He stated that everyone else was taking 2 or 3 seconds.
    > Then he found one other person taking >30 seconds, and it turns out the
    > common denominator was both of us have Intel chips (Celeron) where the other
    > people have AMD.
    >
    > I had him send me his code and he was doing a lot of .resize() in nested
    > vectors.
    >

    Have a look at the code generated for the constructors of whatever is in
    the vector, resize defaults to the object's default constructor to
    initialise the new elements.

    >
    > Before I rewrote it as a class for him I had considered things such as
    > ..clear() .reserve() but that didn't immediately update the .size() for the
    > vector, and I think the class is the best way to go after all.
    >

    reserve doesn't initialise the vector's elements, unlike resize which does.

    --
    Ian Collins.
    Ian Collins, May 31, 2006
    #2
    1. Advertising

  3. Jim Langston

    Jim Langston Guest

    "Ian Collins" <> wrote in message
    news:...
    > Jim Langston wrote:
    >> I really am not sure if this question belongs in this newsgroup, but not
    >> sure where else to ask it.
    >>
    >> There is someone working on a game that I tested, and it was taking >30
    >> seconds to load. He stated that everyone else was taking 2 or 3 seconds.
    >> Then he found one other person taking >30 seconds, and it turns out the
    >> common denominator was both of us have Intel chips (Celeron) where the
    >> other
    >> people have AMD.
    >>
    >> I had him send me his code and he was doing a lot of .resize() in nested
    >> vectors.
    >>

    > Have a look at the code generated for the constructors of whatever is in
    > the vector, resize defaults to the object's default constructor to
    > initialise the new elements.
    >
    >>
    >> Before I rewrote it as a class for him I had considered things such as
    >> ..clear() .reserve() but that didn't immediately update the .size() for
    >> the
    >> vector, and I think the class is the best way to go after all.
    >>

    > reserve doesn't initialise the vector's elements, unlike resize which
    > does.


    Yes, this I know, but right after he did the resize he was iterating through
    all the vectors initializing them anyway. But the issue of the .size() not
    being right caused problems such as this:

    std::vector<int> MyVec;
    MyVec.reserve(100);
    MyVec[50] = 10;

    This worked on my compiler with my settings but barfed on his machine
    stating that it was an array overflow.
    Jim Langston, May 31, 2006
    #3
  4. Jim Langston

    Ian Collins Guest

    Jim Langston wrote:
    > "Ian Collins" <> wrote in message
    > news:...


    >>reserve doesn't initialise the vector's elements, unlike resize which
    >>does.

    >
    >
    > Yes, this I know, but right after he did the resize he was iterating through
    > all the vectors initializing them anyway. But the issue of the .size() not
    > being right caused problems such as this:
    >
    > std::vector<int> MyVec;
    > MyVec.reserve(100);
    > MyVec[50] = 10;
    >
    > This worked on my compiler with my settings but barfed on his machine
    > stating that it was an array overflow.
    >

    It probably should barf, reserve simply reserves memory, it doesn't
    initialise anything else. size() will be unchanged, capacity() will
    reflect the new allocation.

    --
    Ian Collins.
    Ian Collins, May 31, 2006
    #4
    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. keith
    Replies:
    7
    Views:
    5,350
    Clifford Heath
    Aug 4, 2005
  2. Chris Roe
    Replies:
    1
    Views:
    319
    Wee Jin Goh
    Jun 11, 2004
  3. David Geering

    longs, long longs, short short long ints . . . huh?!

    David Geering, Jan 8, 2007, in forum: C Programming
    Replies:
    15
    Views:
    555
    Keith Thompson
    Jan 11, 2007
  4. whatnext
    Replies:
    9
    Views:
    417
  5. Kazik�
    Replies:
    4
    Views:
    1,320
    Jonathan Lee
    Jul 6, 2009
Loading...

Share This Page