Re: [Dev-C++] Optimized memory allocation
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Shane B. <ast...@te...> - 2003-06-20 08:04:16
|
MessageYour 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 -----=20
From: Ioannis Vranos=20
To: 'C++'=20
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]=3D ...
Ioannis Vranos
* Programming pages: http://www.noicys.freeurl.com
* Alternative URL 1: http://run.to/noicys
* Alternative URL 2: http://www.noicys.cjb.net=20
-----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 loading 48k records from a MySQL SELECT statement, which has =
a 32 char primary key. One of the more intensive processes involved is =
calling "new" to create a new object 48k times. In standard C, I would =
malloc(RowCount * sizeof(class_type)), and using pointer math, simply =
iterate whenever I needed a new structure. With C++, a call to new is =
required for construction purposes. Is there an optimized method =
wherein I can allocate a dynamic number to be preallocated more =
efficiently all at once in one call?
I have considered using "new class_type[RowCount]()" to allocate =
an array (haven't tried to see if that would actually work), but I was =
wondering if there was a more efficient means to preallocate objects.
Thanks,
-Shane |