A question about the performance of htmlText in TextField - apache-flex

I have got a performance problem about TextField.htmlText +=msg.And
I know thatTextField.appendText(msg) works better than TextField.text +=msg.So I wonder if there's some method better than TextField.htmlText +=msg?Any help would be appreciated.
Regards
Spawn

I haven't benchmarked it, but what I normally do is this:
var str:String = "bla bla";
for(var i:int = 0; i < 10; i++){
str += " foo";
}
myTextfield.htmlText = str;
However, it's likely not that much of a boost unless you're doing large amounts of text and/or iterations.

Concatenate your text in a variable before assigning it to the htmlText property of any control. Every time you change that property you are calling all the lifecycle display methods like commitProperties, measure, and updateDisplayList, all of which take time to render.

Related

CS50, Pset 4, filter-less, reflection issues

so im currently trying to do the less comfortable pset 4 and have just got to the reflection part.
My code seems to work with the examples provided but fails the CS50 Check, I believe its something to do with my logic that i just cant get my head around. Maybe to do with the fact ive not actually done anything regarding whether the width is even or odd.
Also I originally tried to put the * pointer on the "image[i][j]" parts but that didnt work so then decided to do it on the "temp" part and discovered I had to use "malloc". Im not sure why this is the case as in the lecture its the variables we want changing and not the temporary variable that was assigned a pointer, any explanation towards that would also be appreciated.
I've also never used this site before so apologies if this doesnt make sense.
PG x
edit: I've also just realise it 'works' on the images provided even if there are no pointers.
edit: I saw on another question that doing "image[i][width -1 - j]" works, so now it passes the check but im not sure why its a "-1", is it because arrays start at [0] ?
void reflect(int height, int width, RGBTRIPLE image[height][width])
for (int i = 0; i < height; i++)
{
//width / 2 as we swap half the picture with the other half (unless its an uneven width?! :O)
for (int j = 0; j < width / 2; j++)
{
//first pixel = last pixel
//the * means that we are going to the LOCATION of temp in memory rather than just the value itself
RGBTRIPLE *temp = malloc(sizeof(RGBTRIPLE));
*temp = image[i][j];
image[i][j] = image[i][width -j];
image[i][width - j] = *temp;
free(temp);
}
}

Why memcpy NOT work on this set<int> array case?

a is set< int> ARRAY, I want to copy it to b. BUT...
int main(){
set<int> a[10];
a[1].insert(99);
a[3].insert(99);
if(a[1]==a[3])cout<<"echo"<<endl;
set<int> b[10];
memcpy(b,a,sizeof(a));
if(b[1]==b[3])cout<<"echo"<<endl;// latch up here, what happen?
return 0;}
Do you know What is computer doing?
I assume the 'set' class you are using is a std::set? What makes you think that simplying memcpying the raw bytes of a std::set (or array of them, in this case) will work properly? This is highly dependent on the internal structure and implementation of the set class, and trying to do such a thing with anything more complicated than a primitive or array of primitives is almost guaranteed to give unexpected results. Doing this sort of raw byte manipulation when classes are involved is rarely going to be correct.
To do this properly you should iterate over the sets and use their '=' operator to assign them, which knows how to copy the contents properly:
for(int i = 0; i < 10; ++i) {
b[i] = a[i];
}
Even better you can use std::copy:
std::copy(std::begin(a), std::end(a), std::begin(b));

Is there a way to convert QDomNode text to QString?

I have a QDomDocument called doc and I need to make a QStringList of each of its top-level children. So far I have this:
QDomNodeList nodes = doc.childNodes();
for(int i = 0; i < nodes.size(); i++)
{
QDomText text = nodes.at(i).toText();
//do something here...
}
However, I cannot find any way to convert this to a QString. This is my first time ever working with The QDom* classes, so I'm pretty confused, and going by the docs there doesn't seem like much can be done with QDomText. Can anyone please offer some advice? Thanks in advance!
The parent QDomElement has a method QDomElement::text() returning the contents of the text node. Operating on the QDomElement level and using text() should be the most simple and standard way to get the element text.
Alternatively, you can call QDomCharacterData::data() (QDomCharacterData is a baseclass of QDomText) in cases where you go down to the QDomText node level.

Raw data to QImage

I'm new to graphics programming (pixels, images, etc..)
I'm trying to convert Raw data to QImage and display it on QLabel. The problem is that, the raw data can be any data (it's not actually image raw data, it's binary file.)
The reason if this is that, to understand deeply how pixels and things like that work, I know I'll get random image with weird results, but it will work.
I'm doing something like this, but I think I'm doing it wrong!
QImage *img = new QImage(640, 480, QImage::Format_RGB16); //640,480 size picture.
//here I'm trying to fill newly created QImage with random pixels and display it.
for(int i = 0; i < 640; i++)
{
for(int u = 0; u < 480; u++)
{
img->setPixel(i, u, rawData[i]);
}
}
ui->label->setPixmap(QPixmap::fromImage(*img));
am I doing it correctly? By the way, can you point me where should I learn these things? Thank you!
Overall it's correct. QImage is a class that allows to manipulate its own data directly, but you should use correct pixel format.
A bit more efficient example:
QImage* img = new QImage(640, 480, QImage::Format_RGB16);
for (int y = 0; y < img->height(); y++)
{
memcpy(img->scanLine(y), rawData[y], img->bytesPerLine());
}
Where rawData is a two-dimension array.
This is how I saved a raw BGRA frame to the disk:
QImage image((const unsigned char*)pixels, width, height, QImage::Format_RGB32);
image.save("out.jpg");
Syntactically, your code appears to be correct.
Reading the class signature, you may want to call setPixel in the following manner:
img->setPixel(i, u, QRbg(##FFRRGGBB));
Where ##FFRRGGBB is a color quadruplet, unless, of course, you want monochrome 8 bit support.
Additionally, declaring a naked pointer is dangerous. The following code is equivalent:
QImage image(640, 480, QImage::Format_something);
QPixmap::fromImage(image);
And will deallocate appropriately upon function completion.
Qt Examples directory is a great place to search for functionality. Also, peruse the class documentation because they're littered with examples.

How to get a QVector<T> from a QVector<QVector<T>>?

I've got a QVector of QVector. And I want to collect all elements in all QVectors to form a new QVector.
Currently I use the code like this
QVector<QVector<T> > vectors;
// ...
QVector<T> collected;
for (int i = 0; i < vectors.size(); ++i) {
collected += vectors[i];
}
But it seems the operator+= is actually appending each element to the QVector. So is there a more time-efficent usage of QVector or a better suitable type replace QVector?
If you really need to, then I would do something like:
QVector< QVector<T> > vectors = QVector< QVector<T> >();
int totalSize = 0;
for (int i = 0; i < vectors.size(); ++i)
totalSize += vectors.at(i).size();
QVector<T> collected;
collected.reserve(totalSize);
for (int i = 0; i < vectors.size(); ++i)
collected << vectors[i];
But please take note that this sounds a bit like premature optimisation. As the documentation points out:
QVector tries to reduce the number of reallocations by preallocating up to twice as much memory as the actual data needs.
So don't do this kind of thing unless you're really sure it will improve your performance. Keep it simple (like your current way of doing it).
Edit in response to your additional requirement of O(1):
Well if you're randomly inserting it's a linked list but if you're just appending (as that's all you've mentioned) you've already got amortized O(1) with the QVector. Take a look at the documentation for Qt containers.
for (int i = 0; i < vectors.size(); ++i) {
for(int k=0;k<vectors[i].size();k++){
collected.push_back(vectors[i][k]);
}
}
outer loop: take out each vector from vectors
inner loop: take out each element in the i'th vector and push into collected
You could use Boost Multi-Array, this provides a multi-dimensional array.
It is also a 'header only' library, so you don't need to separately compile a library, just drop the headers into a folder in your project and include them.
See the link for the tutorial and example.

Resources