Flash CS3: TextField.appendText ()

Flash CS3 has very many innovations, including the "curious" to indicate at the time of compiling some tips on writing code. This happens, for example, when we are dealing with objects TextField . I've had to add a string to the end of a field TextField with the classical notation:

1
+ = "Stringa aggiunta" ; mioTextField_txt. text + = "string adding";

But here is that ActionScript 3.0, when completing the movie, I felt (Warning) - or recommended:

Warning: 3551: Adding text to a TextField using + = is many times slower than using the TextField.appendText () method.

Cool! Let's see the fact of the method SYNOPSIS appendText() actually says:

) applicata a una proprietà text (ad esempio <strong>someTextField.text += moreText</strong> ), in particolare nel caso di un campo di testo con una quantità di contenuto significativa. Appends the string specified by newText at the end of the text field. This method is more efficient than an addition assignment ( += ) properties applied to a text (eg <strong>someTextField.text += moreText</strong> ), in particular in the case of a text field with a significant amount of content.

We trust? To verify security:

1
2
3
4
5
6
7
8
9
. getTimer ; flash.utils imports. getTimer;

uint = 0 ; var i: uint = 0;
int = getTimer ( ) ; var s: int = getTimer ();
i ; i < 10000 ; i ++ ) { for (i, i <10000; i + +) {
+ = "Ciao" ; text_txt. + text = "Hello";
}
int = getTimer ( ) ; var f: int = getTimer ();
f - s ) ; trace (f - s);

Time: 9360 - on my machine in debug mode. Let's try as advised by the compiler:

1
2
3
4
5
6
7
8
9
. getTimer ; flash.utils imports. getTimer;

uint = 0 ; var i: uint = 0;
int = getTimer ( ) ; var s: int = getTimer ();
i ; i < 10000 ; i ++ ) { for (i, i <10000; i + +) {
( "Ciao" ) ; text_txt. appendText ("Hello");
}
int = getTimer ( ) ; var f: int = getTimer ();
f - s ) ; trace (f - s);

Time: 8415 ;)

Nothing fancy ... but it's true! ). The method appendText() is faster and more powerful pre-incremental operator ( += ). Why? The most obvious reason is as follows: the operator ( += ) works on any type of data. Moreover, in an OO language this operator is not structured as in C but is simply an "object". In C + +, for example, you can overwrite (override) the operator as part of the structure of the object language itself. Ultimately appendText() is faster because its goal is to "hang" a string and thus does not do many "questions" in it. The operator ( += ), however, should work for any type of data and therefore is not optimized for strings. In C, on the contrary, the operator ( += ) is a function of low-level and will always be faster than a call to a function.

There are no comments for this post

Leave a comment

XHTML TAG PERMIT: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> INSERTION CODE:
 <pre></pre> // blocco generico <code></code> // blocco generico [cc_actionscript][/cc_actionscript] // Actionscript [cc_actionscript3][/cc_actionscript3] // Actionscript 3 [cc_css][/cc_css] // CSS Style Sheet [cc_html][/cc_html] // HTML [cc_js][/cc_js] // Javascript [cc_objc][/cc_objc] // Objective-C [cc_php][/cc_objc] // PHP [cc_sql][/cc_sql] // SQL