开发者

var a:Number = 5 vs var a=5 (ActionScript)

开发者 https://www.devze.com 2023-03-02 08:50 出处:网络
Last night I thought about this question in Flash var a:Number = 5; var a = 5; What declaration is better? Does first declaration work faster and take less memory?

Last night I thought about this question in Flash

var a:Number = 5;
var a = 5;

What declaration is better? Does first declaration work faster and take less memory? All examples of AS contain first declaration method(e.g. http://actionscriptexamples.com/2010/05/03/getting-started-with-the-tlftextfield-class-in-actionscript-3-0-and-flash-cs5/).

var tlfTxt:TLFTex开发者_Python百科tField = new TLFTextField();

Why do they use first method instead of second?


Also think about optimization. Even though you can declare your variable with no type, like

var a:* = 5, what can be used awesomely, still consider the following optimization test:

var a = 5;
var b:* = 5;
var c:Number = 5;
var d:int = 5;
var e:uint = 5;

var time_start:Number = getTimer();

for(var i:int = 0; i < 10000000; i++)
{
    a += 1;
    //b += 1;
    //c += 1;
    //d += 1;
    //e += 1;
}

var time_stop:Number = getTimer();

trace(time_stop - time_start, "ms");

//a.) 650 ms
//b.) 651 ms
//c.) 106 ms
//d.) 69 ms
//e.) 69 ms

The results show each round on an average machine. Whatever the numbers are exactly in the example above, the point is to see the rate:

In cases a and b, we didn't give a type to the variable. In the third case (c.) we gave it a type, and we reached a 6.14 times speed increase.

In cases d and e, you can see what happens if you really set the type specifically: integer and unsigned integers. This case, so if you use integers, the speed you can gain is really big (10x).

Think about this when writing random functions, big calculations or complex engines.


Always define a type. As mentioned, if you don't the compiler will choose for you, and it may not be as expected.

It also becomes important when dealing with classes. For example you create a private variable foo in your class, such as:

private var _foo; // no type is defined

Later down the track, you want to add a get/set property for it:

public function get foo():??? { return _foo; }
public function set foo(val:???):void { _foo = val; }

What would you be passing in? And how do you document this for someone else to use?

Where as if you had

private var _foo:Number;

public function get foo():Number { return _foo; }
public function set foo(val:Number):void { _foo = val; }

It is clear, straight away what foo does in both the get/set cases.

And one more thing. If you want to create a MovieClip variable which references something on the stage, say:

var mymc = stage_mc; // Hmmm, not so good

If you wanted to call a MovieClip method on mymc, say play(), the compiler will throw an error because it doesn't 'know' mymc has a play method (how would it?). So by typing it (in the sense of adding a type to it, like MovieClip), you are letting the compiler know what methods and properties should be available.

Example:

var mymc:MovieClip = stage_mc; // Ahh, much better!

It also alows the compiler to do type checking. So in the above example, if you tried to pass in a textfield to the movieclip variable mymc, an error will throw an error. This stops bigger bugs sneaking in.

I'm very passionate about type checking in AS3 - I'm glad Flash finally introduced it, and now that it's here, let's use it!


In AS3 there is nearly no good reason to not explicitly type something. There are cases when it is appropriate, but the type system is there to prevent problems.

As for speed / memory differences, this is a pretty good explanation: http://onflex.org/ACDS/AS3TuningInsideAVM2JIT.pdf


In a language with strong static typing like AS3 you should always be typing your objects at decleration, it will make your code cleaner and help you track down bugs easier.

That said, AS3 is usually quite forgiving and is able to figure out which type you need at runtime. For example, this snippet works fine:

    private var foo;

    public final function run():void {

        bar();
        baz();
    }

    private function bar():void {

        foo = 4;
        foo -= 0.5;
        trace( foo ); // traces "3.5"
    }

    private function baz():void {

        foo = "qux";
        trace( foo ); // traces "qux"
    }


in the 2nd statement the compiler has to decide which type he has to use. i.e 5 is an integer he also could assume you mean

var a:int = 5;  or  var a:uint = 5;

also for code completion purposes it's better do define a type ;-) but that's not a real reason to do, because some IDE's can handle these too.

0

精彩评论

暂无评论...
验证码 换一张
取 消