Dynamic memory or...?

T

trouble

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.
 
M

Moonlit

Hi,


trouble said:
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.
 
V

Victor Bazarov

trouble said:
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
 
G

Gianni Mariani

trouble said:
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.
 
J

Jerry Coffin

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,826
Members
47,371
Latest member
Brkaa

Latest Threads

Top