Re: C++ is Slow?
On Feb 6, 3:28 pm, nw <n...@soton.ac.uk> wrote:
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. 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.
The trick is for operator[] to return a proxy object, which
supports operator[] to access the final value. As it happens,
T* fits the bill (although there'll be no bounds checking), so
implementing two dimensional arrays is particularly simple.
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?
class Vector2D
{
public:
Vector2D( size_t i, size_t j )
: m( i )
, n( j )
, data( i * j )
{
}
double* operator[]( size_t i )
{
return &data[ i * m ] ;
}
// ...
private:
size_t m ;
size_t n ;
std::vector< double > data ;
} ;
For starters.
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.
The nice thing about using a class like the above is that you
can change the implementation at will without any changes in the
client code.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34