[Kstars-devel] Porting to Qt4: avoid pointers in QLists?

Thomas Kabelmann thomas.kabelmann at gmx.de
Mon Sep 12 10:15:59 CEST 2005


Hi Jason,

I couldn't compile your test program, because my system is currently not up to 
date, but after reading the documentation I think it's better to use a list 
of pointers.

As the QList internally stores an array of pointers, it's obvious, that a list 
of pointers will be faster. Let me explain why:

sp1.append( SkyPoint( 0.01*float(i)/15.0, 0.0 ) );
The SkyPoint will be initialized on the stack, the list makes a copy on the 
heap and then stores a pointer to the copy of the SkyPoint.

sp2.append( new SkyPoint( 0.01*float(i)/15.0, 0.0 ) );
The SkyPoint is allocated on the heap from the beginning and the list just 
stores the pointer to it. No extra copy is done.

The same is for the [ ] operator.

sp1[i].EquatorialToHorizontal( &LST, &lat );
Here the [ ] dereferences the pointer and then returnes a reference to the 
SkyPoint.

sp2[i]->EquatorialToHorizontal( &LST, &lat );
And here you just get the pointer to the SkyPoint, which indeed seems to be 
much faster.

Btw, could you just try, if at() instead of [ ] is faster for our case, as the 
doc mentioned: "at() can be faster than operator[](), because it never causes 
a deep copy to occur."

Regards,
Thomas


More information about the Kstars-devel mailing list