PDA

View Full Version : are they similar in performance ?



Gopala Krishna
12th January 2008, 05:42
I have usage like


foreach(Item *, items()) {
// operation a on item
// operation b on item
// operation c on item
}

Now these operations a, b, c span multiple lines and the code looks messy.
So i thought i can split them into different functions and the function can't be written in the form function(one item) instead the functions have to be of form function(ItemList)

My question is will the performance remain same even if i use it this way
(the number of items will be in hundreds and in worst case thousands)

functionA(ItemList list) {
foreach(Item *, list) {
operation
}
}

functionB(ItemList list) {
foreach(Item *, list) {
operation
}
}

functionC(ItemList list) {
foreach(Item *, list) {
operation
}
}

// --------------------and finally in the client code
functionA(list);
functionB(list);
functionC(list);

spud
12th January 2008, 11:43
The short answer i no, since foreach() copies the container returned by items(). This doesn't have to be a severe penalty since QList supports implicit sharing (http://doc.trolltech.com/4.2/shared.html). If your code looks messy, why don't you put operation a, b and c in inlined functions, which you hide frome the client by making them private members or like this:



namespace{
inline void operationA(Item* item){...}
inline void operationB(Item* item){...}
inline void operationC(Item* item){...}
}

void publicFuntion()
{
foreach(Item *, items()) {
operationA(item);
operationB(item);
operationC(item);
}
}

That way your code will remain clean aswell as the public interface.

fullmetalcoder
12th January 2008, 11:55
The short answer is NO. As you do three loops instead of one it'll take three times as much as it used to... If yo are doing this frequently and on big lists the first alternative is recommended, unless performance does not matter in your application. The solution offered by spud is probably the best as it clearly separate different operations and still performs a single loop :)

Gopala Krishna
12th January 2008, 19:05
Thanks for your replies but it is better i post code so that i can highlight fact that i can't write a function for single item.
I have the following kind of usage occurring few times.


QRectF rect = m_insertibles.isEmpty() ? QRectF() : m_insertibles.first()->sceneBoundingRect();

foreach(QucsItem *item, m_insertibles) {
item->setSelected(true);
item->setVisible(cursorOnScene);
rect |= item->sceneBoundingRect();
}

QPointF center = rect.center();
QPointF delta = active->mapToScene(pos) - center;

foreach(QucsItem *item, m_insertibles) {
item->moveBy(delta.x(), delta.y());
}

Well i thought i could write convienience functions like say


QPointF centerOfItems(const QList<QGraphicsItem*> &_list);
void moveItemsBy(QList<QGraphicsItem*> &_list, QPointF delta);
void setVisibilityofItems(QList<QGraphicsItem*> &_list, bool visiblity);

But as you can see the three implementations will have repetitions of loop. Ofcourse trying to be optimistic, the code isn't that clumsy ;)