C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

slowdown from calling new

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
goetz.leonhardt@gmail.com
Guest





PostPosted: Fri Feb 24, 2006 7:06 pm    Post subject: slowdown from calling new Reply with quote



Hi,

I have a performance problem with a class which looks like

class slow{
public:
slow(): vec(100,0) {}

std::vector<int> vec;
};

The solution is

class fast{
public:
fast()
{
memset(vec,0,100);
}
int vec[100];
};

In my application automatic objects are created and destroyed very
often. So putting the memory on the stack as in class fast is faster
than in class slow. However, I cannot implement class fast, because it
requires a hardcoded number 100, known at compile time I believe.
slow() on the other hand always causes a call to new, which causes the
slowdown of the program.

Any idea how else I might be able to speed up slow() ?

Thanks in advance,
Goetz


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Daniel T.
Guest





PostPosted: Sat Feb 25, 2006 12:06 pm    Post subject: Re: slowdown from calling new Reply with quote



In article <1140799937.482576.269450 (AT) p10g2000cwp (DOT) googlegroups.com>,
"goetz.leonhardt (AT) gmail (DOT) com" <goetz.leonhardt (AT) gmail (DOT) com> wrote:

Quote:
Hi,

I have a performance problem with a class which looks like

class slow{
public:
slow(): vec(100,0) {}

std::vector<int> vec;
};

The solution is

class fast{
public:
fast()
{
memset(vec,0,100);
}
int vec[100];
};

In my application automatic objects are created and destroyed very
often. So putting the memory on the stack as in class fast is faster
than in class slow. However, I cannot implement class fast, because it
requires a hardcoded number 100, known at compile time I believe.
slow() on the other hand always causes a call to new, which causes the
slowdown of the program.

Any idea how else I might be able to speed up slow() ?

Yes. But first, please understand that in both cases you have the vector
hardcoded to 100 elements, the difference is that vector can grow if
needed whereas the int array can't (as well as where those elements are
kept in RAM.)

So... if you might sometimes need more than 100 elements, the easiest
way to speed up slow is to start with an empty vector:

class faster_slow {
vector<int> vec;
public:
faster_slow(): vec() { }
};

The objects are created much faster because no new is necessary, but
they can grow as needed (maybe you don't need 100 elements every time.)


--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Ron Natalie
Guest





PostPosted: Sun Feb 26, 2006 12:06 pm    Post subject: Re: slowdown from calling new Reply with quote



goetz.leonhardt (AT) gmail (DOT) com wrote:
Quote:
Hi,

I have a performance problem with a class which looks like

class slow{
public:
slow(): vec(100,0) {}

std::vector<int> vec;
};


Are you sure you turned on the optimizer?
The library templates make a lot of use of inlining.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Thomas Richter
Guest





PostPosted: Sun Feb 26, 2006 5:06 pm    Post subject: Re: slowdown from calling new Reply with quote

goetz.leonhardt (AT) gmail (DOT) com wrote:
Quote:
Hi,

I have a performance problem with a class which looks like

class slow{
public:
slow(): vec(100,0) {}

std::vector<int> vec;
};

The solution is

class fast{
public:
fast()
{
memset(vec,0,100);
}
int vec[100];
};

In my application automatic objects are created and destroyed very
often. So putting the memory on the stack as in class fast is faster
than in class slow.

Cough, cough. This way, the memory is put into the same memory as the
object. Whether that's stack or heap depends where the object is put.

Quote:
However, I cannot implement class fast, because it
requires a hardcoded number 100, known at compile time I believe.
slow() on the other hand always causes a call to new, which causes the
slowdown of the program.

Any idea how else I might be able to speed up slow() ?

Even if the number of elements in your array varies, do you know this
number at the time the object is constructed? Do you need to enlarge the
object dynamically while the program runs or stays the array size fixed
after allocation?

Possible solutions (depending on your side-conditions):

a) template your class by the number of elements. (Must know the maximal
size at compile time, though the number of elements may vary).
b) allocate memory for an "old-style" int array and place an int *
into your class, along with a number of elements. (Need not to know
the number of elements, is possibly faster when iterating over the
elements for some low-level operations)
c) Use "lazy aquisition", i.e. delay the allocation of the array/the
vector up to until it is used the first time, (works in either case,
might cause slowdown in the interface methods that need to check whether
the vector is already there)
d) keep a small array within the class, along with a vector. Write
a small interface that first uses the ints in the class, and if this
array overruns, switches to the vector by copying the data over. (works
in either case, but requires extra care for the interface)

So long,
Thomas


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Valentin Samko
Guest





PostPosted: Sun Feb 26, 2006 9:06 pm    Post subject: Re: slowdown from calling new Reply with quote

goetz.leonhardt (AT) gmail (DOT) com wrote:
Quote:
memset(vec,0,100);
int vec[100];

This is yet another example which shows that it is much easier to write buggy code if you
use C arrays, and not STL. If you know the size of your array at compile time, it is
better to use boost::array than C arrays due to the same reason.

--

Valentin Samko - http://www.valentinsamko.com

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Edson Manoel
Guest





PostPosted: Fri Mar 03, 2006 1:06 am    Post subject: Re: slowdown from calling new Reply with quote

Quote:

Any idea how else I might be able to speed up slow() ?


Create a memory pool (e.g, using a big buffer or alloca() function) and
use a custom allocator for the STL containers.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.