RE: [Dev-C++] Optimized memory allocation
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Louis G. <lg...@ya...> - 2003-06-21 12:54:28
|
Ioannis, What's the difference betweem the 3rd edition and the special edition ? Which one do you recommend for someone who knows the basics of C++? --- Ioannis Vranos <iv...@em...> wrote: > #include <vector> > > > // ... > > using std::vector; > > // ... > > vector<my_class>array; > > > > // Reserves space for 100 "my_class"es, does not create any objects. > array.reserve(100); > > > // Creates a my_class at the end copying the value using the assignment > operator. > // At push_back i passed a temporary here. > array.push_back(my_class(7) ); > > > // Equivalent to realloc(). array[0] remains my_class(7) > // and 9 more objects get created with the default constructor. > > array.resize(10); > > > array.erase(array.begin()+6); > > > // Vector (random access) iterators behave like pointers > vector<my_class>::iterator p=array.begin(); > > > //array.end() points one after the end > > array.erase(p+3); > > > // array.size() returns the number of objects > //array.capacity() the number of reserved object space > > > > It is a long story, you have to read vectors. :) I suggest "The C++ > Programming Language" 3rd Edition or Special Edition by Bjarne > Stroustrup > (the creator of C++). Keep in mind that you must have finished some > introductory book first. Check this: > > http://www.research.att.com/~bs/3rd.html > > > > > Ioannis Vranos > > * Programming pages: http://www.noicys.freeurl.com > <http://www.noicys.freeurl.com/> > * Alternative URL 1: http://run.to/noicys > * Alternative URL 2: http://www.noicys.cjb.net > <http://www.noicys.cjb.net/> > > > > > > > > -----Original Message----- > From: Shane Bryldt [mailto:ast...@te...] > Sent: Friday, June 20, 2003 11:04 AM > To: Ioannis Vranos > Cc: C++ > Subject: Re: [Dev-C++] Optimized memory allocation > > > Your idea suggest a significant performance boost, if I was referencing > the > memory with accessing optimization. The issue here, is accessing, it's > allocation performance. > As I believe you meant, > "new class_type[count]" > is equivilent to, > "malloc(count * sizeof(class_type))" > This is the fastest method I can determine for allocation, however the > issue > is that I want to allocate this memory to be used and delete'd not as an > array, but perhaps individually. What I mean is, currently the memory > is > allocated with many calls to "new", whereas one call to new, allocating > all > the memory at once would be faster. However, I cannot take a pointer to > a > specific element of the array and only delete that memory when it's no > longer needed, I would require a management system of the array. > > Herein lies the problem, the array must be allocated and deleted > entirely. > Multiple calls to "new" is required to ensure each memory is allocated > and > internally tracked separately. I do not want one large chunk of memory > accessed as an array, but rather simply to preallocate the required > memory > all at once for a specific number of objects. > > The actual situation, involves as I said before, loading 48k records > from a > database, and for each, creating an object. I do not want this in an > array, > each object is later stored in an STL map and indexed according to a > unique > value. I just want to preallocate, calling constructors correctly, 48k > objects, and then use one at a time into the map, and may later be > individually deleted out of the map by expiration of individual records. > > Your suggestion to use a vector does not imply allocating with new, as > is > needed for each to be allocated correctly and constructed properly. > There > may not be a better solution than calling new 48k times, unfortunately. > > I thought perhaps at a lower level you could preallocate a whole chunk > of > memory, then with another special call, construct an object in > preallocated > memory. This would speed up as I would just iterate through the > construction of each object after allocating for 48k objects, instead of > calling new 48k times separately allocating each time. > > With using any sort of memory manager to manage an array of preallocated > memory would probably negate the speed increase. > > -Shane > p.s. I am really nitpicking here, the entire process takes about 30 > seconds > on average, running on the same server as the database, on low end home > hardware, 350mhz, 256megs of ram, and a standard 5400rpm IDE drive. > Nothing > special, if there are no lower level calls as I described above, then I > can > live with the loading time. > > ----- Original Message ----- > From: Ioannis Vranos <mailto:iv...@em...> > To: 'C++' <mailto:dev...@li...> > Sent: Thursday, June 19, 2003 8:47 PM > Subject: RE: [Dev-C++] Optimized memory allocation > > > new whatever[SIZE] is equivalent to malloc(SIZE*sizeof(whatever)), that > is > it should boe slower. > > But better use an std::vector: > > > #include <vector> > > // ... > > using std::vector; > > // ... > > vector<class_type> array(48); > > // ... > array[0]= ... > > > > > > Ioannis Vranos > > * Programming pages: <http://www.noicys.freeurl.com> > http://www.noicys.freeurl.com > * Alternative URL 1: <http://run.to/noicys> http://run.to/noicys > * Alternative URL 2: <http://www.noicys.cjb.net/> > http://www.noicys.cjb.net > > > > > > -----Original Message----- > From: dev...@li... > [mailto:dev...@li...] On Behalf Of Shane > Bryldt > Sent: Friday, June 20, 2003 3:39 AM > To: C++ > Subject: [Dev-C++] Optimized memory allocation > > > Hello, > > I have recently been attempting to find ways to speed up the process > of > === message truncated === __________________________________ Do you Yahoo!? SBC Yahoo! DSL - Now only $29.95 per month! http://sbc.yahoo.com |