473,386 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Dynamic memory or...?

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
4 2161
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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: meyousikmann | last post by:
I am having a little trouble with dynamic memory allocation. I am trying to read a text file and put the contents into a dynamic array. I know I can use vectors to make this easier, but it has to...
6
by: chris | last post by:
Hi all, I need to know, what is the difference between dynamic memory allocation, and stack allocation ? 1. If I have a class named DestinationAddress, when should I use dynamic memory...
6
by: Materialised | last post by:
Hi Everyone, I apologise if this is covered in the FAQ, I did look, but nothing actually stood out to me as being relative to my subject. I want to create a 2 dimensional array, a 'array of...
5
by: swarsa | last post by:
Hi All, I realize this is not a Palm OS development forum, however, even though my question is about a Palm C program I'm writing, I believe the topics are relevant here. This is because I...
3
by: Stephen Gennard | last post by:
Hello, I having a problem dynamically invoking a static method that takes a reference to a SByte*. If I do it directly it works just fine. Anyone any ideas why? I have include a example...
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...
1
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was...
14
by: vivek | last post by:
i have some doubts on dynamic memory allocation and stacks and heaps where is the dynamic memory allocation used? in function calls there are some counters like "i" in the below function. Is...
20
by: sirsnorklingtayo | last post by:
hi guys please help about Linked List, I'm having trouble freeing the allocated memory of a single linked list node with a dynamic char* fields, it doesn't freed up if I use the FREE()...
10
by: swornavidhya.mahadevan | last post by:
Which allocation (Static / Dynamic) is suitable for the situation when we are trying to allocate for a overloaded memory when the memory is full and no space to allocate. What will happen if both...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.