Vector vs. array V2.

This is a discussion on Vector vs. array V2. within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by Elysia Really? Did you try to step through with the debugger? figured it out. _SECURE_SCL must occur ...

  1. #31
    Registered User
    Join Date
    Aug 2008
    Posts
    188
    Quote Originally Posted by Elysia View Post
    Really? Did you try to step through with the debugger?
    figured it out. _SECURE_SCL must occur AFTER stdafx.h and BEFORE <vector>

    here are results with VS2005
    Code:
    x = -802947072
    a: 5.844
    x = -802947072
    v: 6.516
    x = -802947072
    d: 6.031

  2. #32
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,013
    I don't see why it should. Depends on what you include inside stdafx.h, and even so, I put them before stdafx.h with no problems.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #33
    Registered User
    Join Date
    Aug 2008
    Posts
    188
    Quote Originally Posted by Elysia View Post
    I don't see why it should. Depends on what you include inside stdafx.h, and even so, I put them before stdafx.h with no problems.
    yeah, not sure either. my stdafx.h only has _WIN32_WINNT, stdio.h, and tchar.h defined.

  4. #34
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,271
    Quote Originally Posted by Daved
    To be equivalent you would set the size in the constructor.
    Looking at the dynamically allocated array version, I think that this would be closer to equivalent for the std::vector version:
    Code:
    V()
    {
        vec.resize(size);
        for(int i = 0; i < size; i++)
        {
            vec[i].resize(size);
            for(int j = 0; j < size; j++)
            {
                vec[i][j] = i * j;
            }
        }
    }
    EDIT:
    Quote Originally Posted by bling
    figured it out. _SECURE_SCL must occur AFTER stdafx.h and BEFORE <vector>
    I just added _SECURE_SCL=0 under "Preprocessor Definitions" in project properties and it worked.
    Last edited by laserlight; 11-25-2008 at 01:56 PM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #35
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> Looking at the dynamically allocated array version, I think that this would be closer to equivalent for the std::vector version <<

    I don't think so (depending on the vector implementation). You're default constructing the vectors and then resizing them. The dynamic version just creates a single uninitialized pointer. I think constructing the vector with the initial size is closer. Of course it made little difference in my timing tests.

  6. #36
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,271
    Quote Originally Posted by Daved
    You're default constructing the vectors and then resizing them. The dynamic version just creates a single uninitialized pointer.
    That's true. The default construction is likely to be very cheap, but still not as cheap as not doing anything at all. In this sense the visual similiarity is misleading.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #37
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    I did timing tests between three vector versions, including initializing in the intializer list, using reserve, and using push_back without reserve. There was no discernible differences in times at all, even when I reduced the amount of time spent on summing.


    We're still talking about times and the tests, but does anybody have any thoughts on the results?

    It seems to me that there are two things to notice. The first, as I mentioned before, is that proper use of a vector is not significantly slower than a dynamic array (and in at least one case is a tiny bit faster). Therefore trying to use a dynamic array for efficiency reasons is a premature optimization that is unlikely to have a noticeable impact on your application.

    The second thing is that if efficiency is a concern, you should make sure to profile your application and be aware of things like Microsoft's added range checking that can have a significant effect on performance. Optimizations like defining _SECURE_SCL to 0 are the real things one should be looking for when making an efficient program.

  8. #38
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    I ll agree that seeing these results it seem that the vector has almost the same performance as an array, so there is no real reason not to use it, since it benefits in many ways.

    That was kind of expected though, since internally vector uses an array. I would find a far more interesting result if we would built a test which would also involve allocating memory. Like start from zero size, reallocate 10 spaces, sum, reallocate, sum etc etc.
    Even more interesting would be if we had the vector add one one element, with push_back, but reallocate the array by some factor. In a way, reallocating an array increases its capacity, but adding an element in a vector increases its size and its capacity by some factor. So they would differ on this point, since the array will have the "right" capacity and the vector will increase its capacity automatically. The array should run faster, but if again the speeds are the same you can say:
    1) If you have a X*Y vector and array the will run in almost the same time
    2) If you have a vector and an array that both increase in a fixed size then again the will run the same

    There is nothing else that you can do with an array, rather than resize it and iterate through it, so we will have a better view.
    Last edited by C_ntua; 11-25-2008 at 05:48 PM.

  9. #39
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,271
    Quote Originally Posted by C_ntua
    Even more interesting would be if we had the vector add one one element, with push_back, but reallocate the array by some factor. In a way, reallocating an array increases its capacity, but adding an element in a vector increases its size and its capacity by some factor.
    Adding an element to a vector using push_back() increases its size by 1. Its capacity may or may not increase. If you want to duplicate such functionality with the dynamic array, then you need to keep track of the size, and will have to use placement new to avoid default constructing elements that are not yet in use. (Recall that vectors can store elements of types without a default constructor.)

    Quote Originally Posted by C_ntua
    So they would differ on this point, since the array will have the "right" capacity and the vector will increase its capacity automatically.
    If you know the right capacity then the vector's reserve() would be available for use, so they need not differ (even though reserve() may only have a negligible effect anyway, as Daved has commented).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 08:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 11:33 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 08:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21