472,959 Members | 1,772 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Dynamaic Memory verses static

Greetings,

I was going through the quake2 source code (as you do) when I noticed
arrays are statically allocated on the stack instead of being allocated
dynamically. Iíve seen this before and know itís for efficiency.

Iím basically questioning the merit of such technique nowadays, for
performance-orientated applications? Is it better to dynamically
allocate a smaller array to fit the memory or to use one huge statically
allocated array? Or even one even larger static array and chop it up at
runtime?

Can you point me to any websites (and yes I have searched) that compare
the differences in performance using both approaches?

Thanks.

Jul 19 '05 #1
6 3229
J Anderson wrote:
Greetings,

I was going through the quake2 source code (as you do) when I noticed
arrays are statically allocated on the stack instead of being allocated
dynamically. Iíve seen this before and know itís for efficiency.

Iím basically questioning the merit of such technique nowadays, for
performance-orientated applications? Is it better to dynamically
allocate a smaller array to fit the memory or to use one huge statically
allocated array?
I don't understand your question here. The 'better' technique would be
the one that fits your needs, I suppose. If you need smaller arrays, you
would allocate smaller ones. If you need one 'huge' array, you'd
allocate a huge one.

Static allocation should be very fast (just adjusting a pointer,
usually), while dynamic allocation may be slower (locating a piece of
memory large enough, preparing it for use and eventual deallocation). It
may make sense to avoid several dynamic allocations when a single
allocation (static or dynamic) can do the job.
Or even one even larger static array and chop it up at
runtime?
Don't you think that's basically what the run-time system does to supply
you with dynamic memory? What makes you think you can do better?

Can you point me to any websites (and yes I have searched) that compare
the differences in performance using both approaches?


You could compare it yourself. I have. I found that allocating dynamic
memory was very fast, and I could not significantly improve on it.

You should keep in mind that 99% of code is *NOT* time-critical, and
most of the time you should be far more concerned with writing clean,
understandable, maintainable, bug-free code than with writing fast code.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #2


J Anderson wrote:

Greetings,

I was going through the quake2 source code (as you do) when I noticed
arrays are statically allocated on the stack instead of being allocated
dynamically. Iíve seen this before and know itís for efficiency.

Iím basically questioning the merit of such technique nowadays, for
performance-orientated applications? Is it better to dynamically
allocate a smaller array to fit the memory or to use one huge statically
allocated array? Or even one even larger static array and chop it up at
runtime?
There is only one way to figure out:
Try it on your compiler.

But chances are good, that the statically allocated array will be faster
on most implementations. How much? That depends on your actual implementation.

Can you point me to any websites (and yes I have searched) that compare
the differences in performance using both approaches?


Try some test codes written by yourself. You might learn something.
You might also search for 'object pooling'.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #3
mjm
From what I have read and experienced myself operator new is not
efficient for
allocating very small objects. Ie. if you must allocate many (tens of
millions) of small objects then it might be worth you while to find
out about alternatives.

I have a structure consisting of about 10 million small objects and
the allocation is much slower than traversal and computation.
Jul 19 '05 #4

"mjm" <sp*******@yahoo.com> wrote in message news:f3**************************@posting.google.c om...
From what I have read and experienced myself operator new is not
efficient for
allocating very small objects. Ie. if you must allocate many (tens of
millions) of small objects then it might be worth you while to find
out about alternatives.


It might be. First try the standard allocators. And if that doesn't
meet your performance requirements you can optimize an operator new
for your particular type taking advantage of what you know (be it,
small, fixed size, etc...) about the type.
Jul 19 '05 #5
mjm wrote:
From what I have read and experienced myself operator new is not
efficient for
allocating very small objects. Ie. if you must allocate many (tens of
millions) of small objects then it might be worth you while to find
out about alternatives.

I have a structure consisting of about 10 million small objects and
the allocation is much slower than traversal and computation.


Actually, the new operator is efficient. Most courses on Operating
Systems will teach you that allocating many small objects is not
efficient as allocating one large {i.e. array} object.

On the platforms that I've programmed on, allocating one big chunk
of memory and dividing it up is a lot more efficient than allocating
a whole bunch of small pieces. The trade-off point depends on the
overhead required to allocate memory.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 19 '05 #6
sp*******@yahoo.com (mjm) writes:
From what I have read and experienced myself operator new is not
efficient for
allocating very small objects. Ie. if you must allocate many (tens of
millions) of small objects then it might be worth you while to find
out about alternatives.

I have a structure consisting of about 10 million small objects and
the allocation is much slower than traversal and computation.


Boost provides a pool allocator for designed for exactly this
situation:

http://boost.org/libs/pool/doc/index.html

Jul 19 '05 #7

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

Similar topics

24
by: Steven T. Hatton | last post by:
In the following code, at what point is S::c fully defined? #include <iostream> using std::cout; using std::endl; using std::ostream; class C { int _v;
13
by: Robert Lario | last post by:
C# verses VB.Net Which Way to go. I am sure this issues has come up before. Please direct me to any good articles that cover this issue. Ideally some neutral assessment. Thanks
1
by: Taylor | last post by:
"Static" is synonymous with "class" with regards to members (methods and fields) right? Sometimes I hear refrence to the "static method" and sometimes its refered to a "static method." Is there...
3
by: Tom Jones | last post by:
I do not understand what is meant when someone states that a given method is "hidden" verses overriden. Would someone please provide a short example of both cases and why you might want to...
15
by: Joe Fallon | last post by:
I would like to know how you can figure out how much memory a given instance of a class is using. For example, if I load a collection class with 10 items it might use 1KB, and if I load it with...
12
by: Joe Narissi | last post by:
I know how to create and use static constructors, but is there a such thing as a static destructor? If not, then how do you deallocate memory intialized in the static constructor? Thanks in...
24
by: Ken | last post by:
In C programming, I want to know in what situations we should use static memory allocation instead of dynamic memory allocation. My understanding is that static memory allocation like using array...
3
by: ak | last post by:
Q1: I am looking to find/build a table of PHP version verses timezonedb version shipped with it. For example, I think that 5.2.0 updated the timezonedb to the 2006.14 version. Is there a table of...
1
by: coppergarden | last post by:
I am new to Java and while I have gotten a grasp on several things, I am still uncertain about static and what it means. I have a call I am needing to make to calculate a running total. I have...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.