473,395 Members | 1,412 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

new/delete vs. vector implementing n-ary tree

I am using a small set of functions that implements an n-ary tree. The
library is disscusses here:

http://groups.google.com/group/comp....274ef3f22fdb90

The nodes of the tree are cretaed and erased by new and delete. I just
read in Modern C++ Design that new/delete can be very inefficient. I
am using this library to search in game trees so I do create and erase
a lot of nodes. Would I be better of by creating a large vector
containing nodes and use this vector to store my tree nodes. Instead
of pointers I could use indices. The unused nodes would be linked
together for easy access and used only when a new node is needed. This
would avoid the use of new/delete. The only drawback is that the
memory is allocated even when it's not used, but this is not really an
issue for my application.

Should I expect a significant gain in performance from this?
Jun 27 '08 #1
5 3175
na***********@gmail.com wrote:
I am using a small set of functions that implements an n-ary tree. The
library is disscusses here:

http://groups.google.com/group/comp....274ef3f22fdb90
>
The nodes of the tree are cretaed and erased by new and delete. I just
read in Modern C++ Design that new/delete can be very inefficient. I
am using this library to search in game trees so I do create and erase
a lot of nodes. Would I be better of by creating a large vector
containing nodes and use this vector to store my tree nodes. Instead
of pointers I could use indices. The unused nodes would be linked
together for easy access and used only when a new node is needed. This
would avoid the use of new/delete.
This amounts essentially to implementing your own dynamic memory management
scheme. You should be able to achieve very similar effects my overloading
new and delete for your node class so that a pooled allocation scheme is
used transparently in the background.
The only drawback is that the
memory is allocated even when it's not used, but this is not really an
issue for my application.

Should I expect a significant gain in performance from this?
You will have to try. Adjusting the memory allocation scheme used for
certain classes can yield significant performance gains. Whether it will do
so in a particular case can only be decided by testing.
Best

Kai-Uwe Bux
Jun 27 '08 #2
On Sun, 13 Apr 2008 16:47:32 -0700 (PDT), na***********@gmail.com
wrote:
>The nodes of the tree are cretaed and erased by new and delete
My gosh! What an abomination. Haven't you heard that a C++ program
never use the keyword "new" and "delete"?
Jun 27 '08 #3
My gosh! What an abomination. Haven't you heard that a C++ program
never use the keyword "new" and "delete"?
I do feel a certain amount of shame but what can I do. There is no
ntree<Tcontainer in the STL so I need to write my own. I am sure
someone more familiar with the insights of the STL allocators could do
a much better job. I am trying to do my best, that is why I am asking
what the best way is to do the allocation.
Jun 27 '08 #4
na***********@gmail.com wrote:
>My gosh! What an abomination. Haven't you heard that a C++ program
never use the keyword "new" and "delete"?

I do feel a certain amount of shame but what can I do. There is no
ntree<Tcontainer in the STL so I need to write my own. I am sure
someone more familiar with the insights of the STL allocators could do
a much better job. I am trying to do my best, that is why I am asking
what the best way is to do the allocation.
The overloaded new and delete for your Node class as suggested by
Kai-Uwe Bux is probably your best option. You can experiment until you
get acceptable performance for your application. Start by just calling
global new and delete from your overloads as a baseline.

You should also learn to ignore plonkers on Usenet :)

--
Ian Collins.
Jun 27 '08 #5
na***********@gmail.com wrote:
>My gosh! What an abomination. Haven't you heard that a C++ program
never use the keyword "new" and "delete"?

I do feel a certain amount of shame but what can I do. There is no
ntree<Tcontainer in the STL so I need to write my own. I am sure
someone more familiar with the insights of the STL allocators could
do a much better job. I am trying to do my best, that is why I am
asking what the best way is to do the allocation.
No, that's just a joke from another thread.

There Razii amuses himself by calling new 10 million, or 100 million,
times in a row. When that doesn't run fast enough, he has just
'proven' that Java code is always faster than C++. Don't bother.

There is a this line between 'never used' and 'rarely used'.
Bo Persson
Jun 27 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Dmitri Zhukov | last post by:
This topic was widely discussed before but somehow I still have no idea how to fix the problem. I've a C++ class which resides inside DLL and has some STL members inside. The EXE uses STL vector...
9
by: Gunnar G | last post by:
Is there anything like vector in STL, that performes deep copy of the elements it contains? I hope this will appear in future releases of STL :)
16
by: cppaddict | last post by:
Hi, I am deleting some objects created by new in my class destructor, and it is causing my application to error at runtime. The code below compiles ok, and also runs fine if I remove the body...
2
by: Stijn Oude Brunink | last post by:
Hello, I want to use the vector class to work with arrays of classes but I seem to get in conflict with the delete operator used in the specific class. The code below gives an assertion?! How is...
9
by: david wolf | last post by:
I want to delete all even numbers in a vector, I am not sure if there's any better way to do it. Following program is how I did it. Look at the part of code beginning from comments: //delete all...
35
by: Jon Slaughter | last post by:
I'm having a problem allocating some elements of a vector then deleting them. Basicaly I have something like this: class base { private: std::vector<object> V;
10
by: ypjofficial | last post by:
Hello All, In my program I am using a pointer to a vector vector<XYZ> * vptr = new vector<XYZ>; and also the XYZ class has a char* as one of its member.I have created all the copy...
7
by: JH Programmer | last post by:
Hi, is there any ways that allow us to delete an element in between? say int_val: 1 int_val: 2 int_val: 3
13
by: Tristan Wibberley | last post by:
Hi I've got implementing overloaded operator new and delete pretty much down. Just got to meet the alignment requirements of the class on which the operator is overloaded. But how does one...
19
by: Daniel Pitts | last post by:
I have std::vector<Base *bases; I'd like to do something like: std::for_each(bases.begin(), bases.end(), operator delete); Is it possible without writing an adapter? Is there a better way? Is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.