PDA

View Full Version : Is QMap efficient in case of frequent read access ?



yellowmat
8th November 2006, 11:05
Hi everybody !

I wonder if the QMap class is efficient enough in case of frequent access to the members it references ... let me give more details.

I have a class called COpenGlTexture which is designed as follow :


class COpenGlTexture
{
public:
COpenGlTexture();
COpenGlTexture(const COpenGlTexture&);

~COpenGlTexture();

COpenGlTexture& operator=(const COpenGlTexture&);

void CreateTexture(const QString& textureName);
void DeleteTexture();
bool IsCreated() const { return m_bIsTextureCreated; }
GLuint Texture() const { return m_nTexture; }

private:
void CopyFrom(const COpenGlTexture&);

private:
bool m_bIsTextureCreated;
GLuint m_nTexture;
};


As my application must display some data, using a specific texture for each data types (a data type is defined in one of my enumeration) I thought to do like this :


// Part of application header's file
public:
enum DataType {Type1=0, Type2};

private:
typedef QMap<DataType, COpenGlTexture> OpenGlTextureMap;
OpenGlTextureMap m_Textures;


What I can also tell is that I want my application to handle many kind of data types, it will just be necessary to add a type in the private enumeration and update the paintEvent function to handle the new type.

It seems (to me) to be good way to handle my datas but I wonder (here we go again) if the use of a QMap will give me good performances as I will have to access my map object value each time (each 100ms) I need to redraw my datas and then consuming some time(perhaps useless) calling to the constructor and affectation operator of my class.

Finally, here are my questions :
* is it a good way to handle my datas ?
* is there any other way to do the same thing ?
* is QMap efficient enough to access each 100 ms each map object value ?
* for the moment I just have to handle 2 data types but in a close futur I may have to handle 3, 4 or much more data types ... same question as previous

Thanks in advance.

lauranger
8th November 2006, 12:05
Hi
As far as I can tell from few tests, if you look after performance of search,
size matters :
Under approximately 50 items, an array/vector of pairs is better
Between 50 and 400 items, an ordered map (like QMap) works better
Over 400, a hash map shows better performance.
But if the usage is running through each item, array/vector of pairs
will be the optimum.
Hth.

yellowmat
8th November 2006, 12:58
How did you test performances ?

lauranger
18th November 2006, 20:15
Hi,
I am a bit late with my answer.
I don't have the code anymore, I guess. But this is what I remember :
I tested by filling randomly generated longs in a container
and in a vector for randomly selecting the ones to search for
and repeated the search tests for some times and measured the times
it had taken.
Hth

lauranger
19th November 2006, 09:20
Ouch, I read your first post too quickly !
It appears that the set of your key has no hole,
as it grows from zero without a gap, and you use
every entry of your virtual map.
Here, obviously, anything more complicated than a "vector"
(an array would suffice, as it is static data --the keys are known
at compile time--) is overkill and
brings its overhead in time and memory consumption.
So my advice is : stick to an array.
Hth