Thread: delete[]

  1. #16
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    37% is a noticeable difference. I wonder what sort of optimization flags you played with in your code.

  2. #17
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by master5001 View Post
    37% is a noticeable difference. I wonder what sort of optimization flags you played with in your code.
    Yes, but if you read some of the other posts, I have a pretty old compiler, and the newer compilers are actually much closer (and vector is even faster in some of the examples).

    Also, my code is the WORST possible scenario for vector, as it does so very little with the value it fetches from the vector.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #18
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I don't remember the entire thread and didn't read through it, but wasn't that a comparison between vector and a statically sized array?

    We're discussing vector versus a dynamic array with new[]/delete[] here.

  4. #19
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Daved View Post
    I don't remember the entire thread and didn't read through it, but wasn't that a comparison between vector and a statically sized array?

    We're discussing vector versus a dynamic array with new[]/delete[] here.
    You are right. I will hack my test-code - but I suspect it will make much less diference [I didn't remember what I did, so it was good of you to remember this].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #20
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Doesn't stack memory work better with your CPU cache, and that's why arrays are faster that dynamic arrays? or is it just the calls to new/delete that slow down dynamic arrays?
    In either case, calling vector::reserve() before you start inserting elements can reduce your new/delete calls to just 1.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  6. #21
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    In the case of replacing new[]/delete[] with vector, you should be able to set an initial size in your vector constructor (just like you do when calling new[]). So there would be no need to call reserve or do any other tricks to reduce the memory allocations.

  7. #22
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Daved View Post
    In the case of replacing new[]/delete[] with vector, you should be able to set an initial size in your vector constructor (just like you do when calling new[]). So there would be no need to call reserve or do any other tricks to reduce the memory allocations.
    But wouldn't that fill the array with default initialized elements? I would think that would slow things down a lot more than just calling reserve().
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  8. #23
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Whether it slows anything down depends on what the array is holding. I guess I'm just talking about a simple replacement that involves using vector and getting rid of the need for delete. I'd consider reserve instead of the constructor to be a premature optimization in this thread.

  9. #24
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by cpjust View Post
    Doesn't stack memory work better with your CPU cache, and that's why arrays are faster that dynamic arrays? or is it just the calls to new/delete that slow down dynamic arrays?
    The CPU cache is based on hash values of cache-line addresses. It has no idea of the difference between stack and heap. Stack might be faster simply because it is accessed more often -- but of course, that's how it should be.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. delete[] problem with release config...
    By mikahell in forum C++ Programming
    Replies: 8
    Last Post: 08-21-2006, 10:37 AM
  2. delete[] without new[]?
    By ChadJohnson in forum C++ Programming
    Replies: 6
    Last Post: 04-04-2005, 11:39 AM
  3. delete[] or delete?
    By X PaYnE X in forum C++ Programming
    Replies: 7
    Last Post: 03-30-2005, 03:16 PM
  4. Memory issue with new[] and delete[]
    By Zarkhalar in forum C++ Programming
    Replies: 24
    Last Post: 08-07-2004, 07:45 AM
  5. delete and delete[]
    By Hunter2 in forum C++ Programming
    Replies: 13
    Last Post: 06-26-2003, 04:40 AM