wayfaerer
30th December 2012, 10:44
I was looking at the Qt source (qrect.h), and I noticed a difference between how QRect and QRectF are implemented: QRect's private members are four numbers representing the top left and bottom right point, while QRectF's private members are four numbers representing a point and a width and height. Why are they implemented differently?
The reason I'm asking is that, for example, if you want to access the width of a rectangle inside a loop, you might do this:
for (int i = 0; i < 100000; ++i)
{
doSomeCalculation(someQRectF.width());
}
This would make sense if using a QRectF, because QRectF::width() just returns the private member variable QRectF::w. Thus, the width isn't being calculated every iteration. However, it would not make sense for QRect, since QRectF::width() calculates the width by subtracting x1 from x2. So ideally the same function using a QRect should look like this:
int rectWidth = someQRect.Width(); // only calculate the width once
for (int i = 0; i < 100000; ++i)
{
doSomeOtherCalculation(rectWidth);
}
I realize this is micro-optimization talk, but, nonetheless, what's the reason for this?
The reason I'm asking is that, for example, if you want to access the width of a rectangle inside a loop, you might do this:
for (int i = 0; i < 100000; ++i)
{
doSomeCalculation(someQRectF.width());
}
This would make sense if using a QRectF, because QRectF::width() just returns the private member variable QRectF::w. Thus, the width isn't being calculated every iteration. However, it would not make sense for QRect, since QRectF::width() calculates the width by subtracting x1 from x2. So ideally the same function using a QRect should look like this:
int rectWidth = someQRect.Width(); // only calculate the width once
for (int i = 0; i < 100000; ++i)
{
doSomeOtherCalculation(rectWidth);
}
I realize this is micro-optimization talk, but, nonetheless, what's the reason for this?