Very slow processing of vectors?

This is a discussion on Very slow processing of vectors? within the C++ Programming forums, part of the General Programming Boards category; reserve() is handy for just such a thing - another potential improvement (though w/o having read in detail all of ...

  1. #31
    Registered User
    Join Date
    Nov 2005
    Posts
    52
    reserve() is handy for just such a thing - another potential improvement (though w/o having read in detail all of the thread, I could be off ) would be to make your vector store pointers rather than full objects - this would have the benefit of NEVER having to call the copy constructors on your objects. Additionally, for LOTS more safety against memory leaks, store boost::shared_ptr<Platform> (or the new TR1 equivalent, if your compiler supports that). That way, you can create the Platform dynamically on the heap. wrap it in the shared_ptr, pass that to the vector, and forget about it. The pointer will be deleted when the last shared_ptr to it goes out of scope.

  2. #32
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Or use a boost:tr_vector, which would be ideal for this application and removes the overhead of shared_ptr.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #33
    Registered User
    Join Date
    Nov 2005
    Posts
    52
    Ah, yeah - forgot about those! Good call.

  4. #34
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Just for completeness, does the Platform class have a copy constructor, copy assignment operator, or any other member variables that were not shown in the initial post? The only data shown was four SHORTS, which I assume (hope) are just short ints. If you are pushing back only 12 instances that use the default copy, I highly doubt that there would be more than one reallocation and that the copying would cause such a dramatic speed decrease.

    Point being that you shouldn't have to worry about reserve when you are reserving space for 12 instances of a class that contains only 4 short ints. Something else caused your slowdown, and you just masked the issue by using reserve.

  5. #35
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    Could be. I noticed after going through all this my actualy program was calling ReadPos more like a few hundred million times. My loop broke somewhere into infinite because I never updated my older code to reflect the newer :/. But that doesnt stop that the vector seemed to cause the greatest speed decrease. When replaced with a static, it went alot faster.

    Anyways, its fixed. Your codes are way above my head -,-. Is their any tutorial that explains your suggestions? -,- *Searches google*
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  6. #36
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> Your codes are way above my head

    Whose code? Most of the code here just uses vector, which you were already using. There are references available for vector and the rest of the standard library that you have hopefully found with your search.

    >> Anyways, its fixed.

    Unfortunately, it just appears to be fixed. Whether it is actually fixed or not we don't know because you haven't dug deep enough to find the details. It is often better to find the root of the problem than merely cover it up. I wouldn't be surprised if you had another "major slowdown" later on if you continue to work on this project.

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

Similar Threads

  1. Vectors
    By naseerhaider in forum C++ Programming
    Replies: 11
    Last Post: 05-09-2008, 08:21 AM
  2. How can i made vectors measuring program in DevC++
    By flame82 in forum C Programming
    Replies: 1
    Last Post: 05-07-2008, 02:05 PM
  3. How properly get data out of vectors of templates?
    By 6tr6tr in forum C++ Programming
    Replies: 4
    Last Post: 04-15-2008, 10:35 AM
  4. file writing crashes
    By test in forum C Programming
    Replies: 25
    Last Post: 08-13-2002, 08:44 AM
  5. Points, vectors, matrices
    By subnet_rx in forum Game Programming
    Replies: 17
    Last Post: 01-11-2002, 01:29 PM

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