macromedia flash actionscript scripting, php, remoting, webservices, c#, javascript
I have just came across an interesting thing.
Maybe you know that if you create a string
like this:
var s = "This is my string.";
that Flash automaticly finds the type and so
Flash will store it as a string.
But if you want to use an string-method, Flash
need to create an wrapper-object for the string that
holds all the string-methods:
s.substr(0, s.length);
The creation of the wrapper-object costs time that
you maybe don't have.
I have founded out that if you create your Strings
with the new-operator your String will be an real
Object and the wrapper-object does not need to
be created anymore.
Look at the Test above, their you can see that the
time difference is more than marginal.
This point is guilty for the methods but not for operations.
The string-operations do not need a wrapper-object and
so it is faster to concenate strings with the plus-operator
that don't created with new.
I don't know the player-code so these are only speculations.
SpeedTest = function ()
{
this.test_arr = new Array();
};
//
SpeedTest.prototype.addTest = function (name_str, test_fnc)
{
this.test_arr.push({name_str:name_str, test_fnc:test_fnc});
};
//
SpeedTest.prototype.run = function ()
{
var t;
for (var i = 0; i < this.test_arr.length; i++)
{
t = getTimer();
this.test_arr[i].test_fnc();
trace(this.test_arr[i].name_str + ' - needed: ' + (getTimer() - t) + 'ms');
}
};
// Number of test-loops
c = 10 * 1000;
// Define our Test-Object
sTest = new SpeedTest();
// Add the Tests
sTest.addTest('String(Methods) with new',
function () {
var s = new String("This is a teststring.");
var dummy;
for (var i = 0; i < c; i++) {
dummy = s.substr(0, s.length);
}
}
);
//
sTest.addTest('String(Methods) without new',
function () {
var s = "This is a teststring.";
var dummy;
for (var i = 0; i < c; i++) {
dummy = s.substr(0, s.length);
}
}
);
//
sTest.addTest('Number(Operations) with new',
function () {
var n = new Number(42);
var dummy;
for (var i = 0; i < c; i++) {
dummy = n * n + 42;
}
}
);
//
sTest.addTest('Number(Operations) without new',
function () {
var n = 42;
var dummy;
for (var i = 0; i < c; i++) {
dummy = n * n + 42;
}
}
);
//
sTest.addTest('String(Operations) with new',
function () {
var s = new String("This is a teststring.");
var dummy;
for (var i = 0; i < c; i++) {
dummy = s + s;
}
}
);
//
sTest.addTest('String(Operations) without new',
function () {
var s = "This is a teststring.";
var dummy;
for (var i = 0; i < c; i++) {
dummy = s + s;
}
}
);
// Run our Tests
sTest.run();
// result
/*
String(Methods) with new - needed: 524ms
String(Methods) without new - needed: 1122ms
Number(Operations) with new - needed: 422ms
Number(Operations) without new - needed: 345ms
String(Operations) with new - needed: 613ms
String(Operations) without new - needed: 415ms
*/
Posted by hOk at June 14, 2003 06:23 PM
Comments (10)
Yes, this seems to be a good strategy, if you do a lot of operations on one object, eg. parsing a long string.
Another way to speed up some String-operations is to use the good old Flash 4 operations.
Posted by at June 14, 2003 11:22 PM
sTest.addTest('Boolean (Operations) with new', function () {
var s = new Boolean(true);
var dummy;
for (var i = 0; i<c; i++) {
dummy = s+s;
}
});
//
sTest.addTest('Boolean (Operations) without new', function () {
var s = true;
var dummy;
for (var i = 0; i<c; i++) {
dummy = s+s;
}
});
Bool(Operations) with new - needed: 243ms
Bool(Operations) without new - needed: 171ms
Posted by jdoc at June 17, 2003 09:32 AM
Thanks for sharing this, man :-) I guess that's the cost of working with such a dynamically typed language.
That's why in Java, when we need to use special methods related to a datatype, we must explictly use that datatype's wrapper class. In JScript.NET, we can do something like this: var String:myString = "A simple text";
I hope that Macromedia implement this feature in the next release of Flash.
Posted by Jonas Galvez at June 17, 2003 10:51 AM
Actually java works very much the same as flash does with native types. Strings, Ints and some others are referred to as "Boxed Objects" this means when you call a method on them they are wrapped (like said above)
-Greg
Posted by Greg Burch at July 15, 2003 01:10 AM
I just tried this:
n = 42;
dummy = 0;
t = getTimer();
for (var i = 0; i
and this:
n = new Number(42);
dummy = 0;
t = getTimer();
for (var i = 0; i
And the n declared without a new is faster. What does that mean?!
Also, there's no point in declaring variables with the 'var' operator outside of functions and loops - they will be treated as normal variables regardless.
Posted by Paul Neave at July 15, 2003 11:34 AM
The reason this is happening is that when it operates on a String the interpreter is actually operating on a primitive. When it is performing a method, it must internally look up the word String to match it with the methods.
You will also notice that it is quicker to use [] instead of new Array(); The reason being is the same. To create an Array with [] maps directly to a primitive where with new it has to match up with the associated word Array.
Posted by Kenny Bunch at July 15, 2003 03:58 PM
If the string is declared with new, then concat is faster than +
sTest.addTest('String(Operations) with new and concat',
function () {
var s = new String("This is a teststring.");
var dummy;
for (var i = 0; i
Posted by Richard Lord at August 15, 2003 12:22 PM
Thank you for the info,
regards, Holger
Posted by hOk at August 15, 2003 02:51 PM
Paul, I'm not sure your code would work properly. The second bit of your code doesn't work on my machine at all.
Posted by Online Coupons at November 1, 2003 10:26 PM
humm JScript.NET do the same
it wrap objects around primitive
this is proper to ECMAscript behaviour not to the implementation of ECMAscript in a particular environment: ActionScript.
in the .NET framework you have also the StringBuilder class and you have to use it in C# if you want to concatenate string, can't do that with only the String class
I prefere largely the object wrapping around of primitive way of doing things, but it's just me ;)
for optimisations, I would urgely advise to optimise only when the code needs it, at the end, not when you start to write some code
1 - write/design code
2 - refactor to clean code and design
3 - if needed, optimise
optimised code are the worst to use, document and maintain.
for the inner working of things in JScript/JScript.NET look at this blog: http://blogs.gotdotnet.com/ericli
Posted by zwetan at December 16, 2003 08:57 AM
|
You can use <code>code that should be highlightned</code> to highlight code! Optionally you can use the attribut language="php|perl|java" within the code tag, otherwise actionscript-highlightning will be used. |
