[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Why does dynamic memory allocation take so long?



I was wondering why dynamic memory allocation took so long?

I am getting back into programming, and thought that a good 
first program to return to was to fool around with finding
prime numbers.  Since I did not know how many possible factors
I would need to generate, I started a type with a long integer
and a memory pointer, and every time I generated a new prime
number I added it to a list.  I discovered that to do this
15,000 times took about 45 seconds.  Since at the time I 
was doing this in order to beat a brute force technique that 
was stupidly looping though 333,333,333 million iterations, 
I found my new routine to be a real dog.  At this point I will
be rewriting to use an array with a depth of 30,000,000, which
takes up about 250mb of memory, but allocates in a snap.

I left out that I am programming in Pascal using GPC.  But I 
don't think this is really relevant as I am sure that my use 
of Pascal's new() somehow calls a linux memory allocation 
routine.  I am guessing that there is some automatic memory
garbage collection scheme that is a real dog to set up.

So I would like to know...

Am I right about a doggish garbage collection scheme?

Why is allocating memory this way so slow?

What are the real limits of using this kind of memory 
allocation in the real world?

By the way, using the memory is just fine once I have it
allocated!

yours,
---
Scotty 



Reply to: