C++ is Slow?

J

Juha Nieminen

Hans said:
Maxx schrieb:
Some C++ functions are using dynamic memory allocation. This is not as
fast as static memory allocation.

Do you actually have some reference to support this?
 
T

terminator

Thanks for all the comments and suggestions, they've been very useful.

The consensus seems to be that yes, iostream is slower than stdio but
that it's largely down to poor implementations. I guess if I want to
know exactly why this is so, I'd need to dig around in the
implementations. Probably the best way to go is use iostreams for
small amounts of IO and write my own custom, possibly platform
specific, IO code when speed is critical.

The array versus vector discussion seems more problematic. A couple of
people made the point that actually it's not down to using vectors,
it's how you use them, it's basically down to allocating large blocks
versus lots of small ones. It's a good point and I'll keep it in mind.

The point was also made that if I decide to allocate large blocks and
calculate indexes then I should wrap this functionally in a class.

base on my own memory, vector construction/destruction is no more than
15 percent slower than dynamic allocation/deallocation of a contigous
sequence of objects and with iteration I could not observe any
meaningful difference between an array and a vector.To my surprise
valarray iteration was 50 percent slower than both of the above.All
these were of course platform specific,but I do not see any general
reason to fancy vector slower than bare arrays ecxept the case of
putting the array on intrinsic stack(that is totally avoiding dynamic
allocation/deallocation) in very special cases.
I
guess it would also make sense to wrap it in a class that has a
similar interface to vector, assuming this can be done? This way I
could pass vector or my own object as a template parameter and easily
switch between the two.

I doubt that in this case you get faster than stl:All what stl is
about is a set of generic classes that wrap different techniques of
representing arrays.
For my own VectorContiguous, I guess operator[] would have to return a
dummy object which could be automatically type converted to the
storage type of the VectorContiguous object? Has this already been
done?

you can find some similar class in std::bitset but it generally slows
down the program and I would not do it for any class unless it is the
only way.
It sounds like if pushed I should be able to create a drop in
replacement for vector that contiguously allocates memory. Whither
this is valuable or not seems to be an open question, and probably
architecture dependent.

vector ***is*** a sequential array(what you want write a class for) on
many platforms (I think all of them)I guess your class wont beat it.

regards,
FM.
 

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,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top