Recently, I've read a very nice article about cast speed in AS3 and made some experimets. While doing my own speed tests, I came up with strange results.
While casting a native class with the as
keyword is clearly faster than function style cast, doing the same test on custom class on the other side, shows that successful function style cast is a little faster then successful as
keyowrd cast in this case.
NOTE - I refined my tests because I used debug version of Flash Player which had some impact on end results.
CustomSprite.as
public class CustomSprite extends Sprite { public function CustomSprite() { super(); } }
Test function:
private function testCastSpeed():void { const COUNT:int = 5000000; var i:int; var initialTime:int; var customSprite:CustomSprite = new CustomSprite(); var sprite:Sprite = new Sprite(); initialTime = getTimer(); for(i = 0; i < COUNT; i++) { Sprite(sprite); } log("Native class function cast: " + (getTimer() - initialTime)); initialTime = getTimer(); for(i = 0; i < COUNT; i++) { sprite as Sprite; } log("Native class as cast: " + (getTimer() - initialTime)); initialTime = getTimer(); for(i = 0; i < COUNT; i++) { CustomSprite(customSprite); } log("Custom class function cast: " + (getTimer() - initialTime)); initialTime = getTimer(); for(i = 0; i < COUNT; i++) { customSprite as CustomSprite; } log("Custom class as cast: " + (getTimer() - initialTime)); }Results:
Hardware | Native class | Custom class | ||
---|---|---|---|---|
Function style | as keyword | Function style | as keyword | |
2.1 Ghz Intel Core 2 Duo, Windows 7Flash Player 10.1 | 170ms | 11ms | 9ms | 11ms |
I have really no idea why those results are so different depending on what type (native or custom) class I'm testing. Maybe someone has the answer?
0 komentarze:
Post a Comment