Any way to speed up "new"?

L

Leslaw Bieniasz

Cracow, 2.11.2004

Hello,

If you can place the objects on the stack instead of the heap, then you will
gain some speed.

Niels Dybdahl

How exactly can I do this? Examples please.
Let's assume my class is


class A
{
public:
A(const int n){V = new double [n]; N=n;}
~A(void){delete [] V;}

private:
double *V;
int N;
};


How can I force it's construction on the stack, instead of on the heap?
(by the way, I seem to have read somewhere that in the case of 32
bit Windows that I use, there is no difference between the stack and the
heap; it's the same memory pool. Isn't this true?)

By the way, the above class is a simple implementation of a vector
class. My question has been motivated by the observation that apparently
the allocation of the conventional "C-type" vector:

int n;
double *a = new double [n];

takes visibly less time that the allocation of the equivalent "OO" vector:

int n;
A *a = new A(n);

Thus, I am afraid nobody can convince me that OOP need not be slower that
"C" programming. Whatever we do, the indirection involved in the OOP
cannot be overcome. But, I am open to any helpful suggestions.

Sincerely,

L.B.

*-------------------------------------------------------------------*
| Dr. Leslaw Bieniasz, |
| Institute of Physical Chemistry of the Polish Academy of Sciences,|
| Department of Electrochemical Oxidation of Gaseous Fuels, |
| ul. Zagrody 13, 30-318 Cracow, Poland. |
| tel./fax: +48 (12) 266-03-41 |
| E-mail: (e-mail address removed) |
*-------------------------------------------------------------------*
| Interested in Computational Electrochemistry? |
| Visit my web site: http://www.cyf-kr.edu.pl/~nbbienia |
*-------------------------------------------------------------------*
 

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
473,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top