Thread: allocating structs within STL vectors

  1. #16
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I didn't mean a vector<double*> I meant using a vector<double> vs. double*

    Bottom line appears to be that I should use vector<double> instead of double*
    A double* and a vector<double> are theoretically just as good but with Visual C++ you may need to use some compiler flags to turn off unnecessary bounds checking for vector.

    I am writing a program that includes functions that must be lighting fast. I'm not knowledgeable in optimization techniques, so any up-front time savings help.
    Don't optimise prematurely. Write a program that works first, and then, if you are not satisfied with the performance, check what exactly is taking too long.

    That said, don't pessimize prematurely, either. Use general optimization techniques that don't make the code less readable, such as passing large objects by reference, upfront.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  2. #17
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    King Mir, you're quoting the wrong person.

  3. #18
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by anon View Post
    A double* and a vector<double> are theoretically just as good but with Visual C++ you may need to use some compiler flags to turn off unnecessary bounds checking for vector.
    I think you need to define some pre-processor symbols to turn on extra checking.
    By default, operator[] shouldn't do any checking; only the at() method should throw an exception if an invalid index is specified.

  4. #19
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Even then, the extra bounds checking that at() does might be worth it. In many cases any performance differences are not noticeable. However, the benefits of using the safer option are real (as this example already shows).

  5. #20
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    It seems so far that we're all just assuming that we know the answer to the below question:
    xyz_size isn't a constant known at compile time is it?
    Not meaning to insult anyone of course.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #21
    Registered User
    Join Date
    Dec 2007
    Posts
    7
    xyz_size is not known ahead of time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. STL Vectors of Vectors
    By Beamu in forum C++ Programming
    Replies: 2
    Last Post: 12-31-2008, 05:23 AM
  2. help using vectors with structs
    By Swordsalot in forum C++ Programming
    Replies: 15
    Last Post: 04-09-2008, 11:14 AM
  3. Array of Vectors amd other STL questions
    By kolistivra in forum C++ Programming
    Replies: 16
    Last Post: 04-12-2007, 09:11 AM
  4. STL Vectors
    By Da-Nuka in forum C++ Programming
    Replies: 2
    Last Post: 02-25-2005, 08:35 PM
  5. STL list::sort() sorting structs
    By hk_mp5kpdw in forum C++ Programming
    Replies: 2
    Last Post: 05-02-2002, 07:23 AM