By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,657 Members | 1,094 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,657 IT Pros & Developers. It's quick & easy.

Dynamic memory or...?

P: n/a
Hi,

I have a program that needs to reserve about 1 to 10 Mb of memory
every time it's run. Only one instance of the program is run at any
given time but it's called about 100 times in succession. Speed is
pretty important.

What are the pros and cons of:

(a) hard coding in a large array into the program, as in float
arr[100000000];
(b) dynamically allocating the array as necessary

I'd prefer (b) for aesthetic reasons but will follow the bottom line
(speed!). G

Given that the data processing on each call will take a couple of
seconds, is malloc / new likely to be a bottleneck?

Thanks,
Ciao.
Jul 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Hi,
"trouble" <th******@hotmail.com> wrote in message
news:5f**************************@posting.google.c om...
Hi,

I have a program that needs to reserve about 1 to 10 Mb of memory
every time it's run. Only one instance of the program is run at any
given time but it's called about 100 times in succession. Speed is
pretty important.

What are the pros and cons of:

(a) hard coding in a large array into the program, as in float
arr[100000000]; On most systems this will either SEGV or just give a stack overflow (both
caused by a stack overflow) (b) dynamically allocating the array as necessary
(b) This is the way to go for arrays this size.

I'd prefer (b) for aesthetic reasons but will follow the bottom line
(speed!). G
In both cases an adrress has to be calculated ar[ offset ] = ar + offset *
sizeof float On the stack ar would be something like SP - x. Dynamically
the value of (SP-z) has to be retrieved for ar. I think stack might be a
little faster. The rest of the calculation must be made in both cases.

I don't think there is much difference, however you could check the assembly
created for the processeor you are working. Or just do a little test, that
would be quite easy to do.

Given that the data processing on each call will take a couple of
seconds, is malloc / new likely to be a bottleneck?
Malloc usually have to find in a free list, so the mallocing itself is
usually slower than allocating something on the stack. You could speed
things up by overloading the new operator. Then if deallocating (delete), do
not deallocate but return it to a list which will be used for subsequent
news. This way your new becomes extremely fast, if er is a lot of
allocating, deallocating. (Do take care for derived types which may use
larger pieces of memory),

Thanks,
Ciao.


Regards, Ron AF Greve.
Jul 19 '05 #2

P: n/a
"trouble" <th******@hotmail.com> wrote...
I have a program that needs to reserve about 1 to 10 Mb of memory
every time it's run. Only one instance of the program is run at any
given time but it's called about 100 times in succession. Speed is
pretty important.

What are the pros and cons of:

(a) hard coding in a large array into the program, as in float
arr[100000000];
(b) dynamically allocating the array as necessary

I'd prefer (b) for aesthetic reasons but will follow the bottom line
(speed!). G

Given that the data processing on each call will take a couple of
seconds, is malloc / new likely to be a bottleneck?


It is unknown (and of course unspecified by the language).
You can only know it by testing your program after it has
been written.

Victor

Jul 19 '05 #3

P: n/a
trouble wrote:
Hi,

I have a program that needs to reserve about 1 to 10 Mb of memory
every time it's run. Only one instance of the program is run at any
given time but it's called about 100 times in succession. Speed is
pretty important.

What are the pros and cons of:

(a) hard coding in a large array into the program, as in float
arr[100000000];
(b) dynamically allocating the array as necessary

I'd prefer (b) for aesthetic reasons but will follow the bottom line
(speed!). G

Given that the data processing on each call will take a couple of
seconds, is malloc / new likely to be a bottleneck?


On any modern OS, there is virtually no different.

prog1

float arr[100000000];

int main()
{
}

prog2

float * arr = new float[100000000];

int main()
{
}

but this one might be a bit slower

float arr[100000000] = { 1.0 };

int main()
{
}

The issue is that you'll page fault approx 10,000 times, your TLB will
thrash and this will be way more significant that the system call to
aquire memory. Even the cost of loading the program is way more
significant that the cost of aquiring memory.

As the other poster suggested. Try it and see.
Jul 19 '05 #4

P: n/a
In article <5f**************************@posting.google.com >,
th******@hotmail.com says...
Hi,

I have a program that needs to reserve about 1 to 10 Mb of memory
every time it's run. Only one instance of the program is run at any
given time but it's called about 100 times in succession. Speed is
pretty important.


In that case, you're probably better off rewriting the program allocate
the memory, and then run in a loop and re-use that memory about 100
times instead.

Once you've done that, run a profiler on it and see whether malloc is
causing a bottleneck, but given 100 iterations of operations on around
10 megabytes of memory, my guess is that it won't be.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.