Created
December 26, 2013 11:28
-
-
Save gonchar/8132598 to your computer and use it in GitHub Desktop.
it's not about a deep optimization. That test shows you that you can freely write more readable code and it will be fast. the winner if(a == null) a = new Object(); don't use a||=new Object(); a = a || new Object(), because it looks like |\||\|\+_)+_)(_(*(^&(*&%&*^%$%^$ :D
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package { | |
import flash.display.Sprite; | |
import flash.text.TextField; | |
import flash.text.TextFieldAutoSize; | |
import flash.utils.getTimer; | |
public class ConditionTest extends Sprite { | |
public function ConditionTest() { | |
testTime(); | |
} | |
/** | |
* My results: | |
func1: 1951 | |
func2: 1921 | |
func3: 1931 | |
func4: 1901 | |
func5: 1907 | |
func6: 1929 | |
func7: 1928 | |
*/ | |
private function testTime():void { | |
var timeText:TextField = new TextField(); | |
timeText.autoSize = TextFieldAutoSize.LEFT; | |
timeText.wordWrap = true; | |
addChild(timeText); | |
var i:uint; | |
var count:uint = 30000000; | |
var t:Number; | |
var result:Number; | |
var a:Sprite = new Sprite(); | |
t = getTimer(); | |
for (i = 0; i < count; i++) { | |
a ||= new Sprite(); | |
} | |
result = getTimer()-t; | |
timeText.appendText("func1: "+result+"\n"); | |
t = getTimer(); | |
for (i = 0; i < count; i++) { | |
if (!a) a = new Sprite(); | |
} | |
result = getTimer()-t; | |
timeText.appendText("func2: "+result+"\n"); | |
t = getTimer(); | |
for (i = 0; i < count; i++) { | |
a = a || new Sprite(); | |
} | |
result = getTimer()-t; | |
timeText.appendText("func3: "+result+"\n"); | |
t = getTimer(); | |
for (i = 0; i < count; i++) { | |
if (a == null) a = new Sprite(); | |
} | |
result = getTimer()-t; | |
timeText.appendText("func4: "+result+"\n"); | |
t = getTimer(); | |
for (i = 0; i < count; i++) { | |
if (a != null) { | |
} else { | |
a = new Sprite(); | |
} | |
} | |
result = getTimer()-t; | |
timeText.appendText("func5: "+result+"\n"); | |
t = getTimer(); | |
for (i = 0; i < count; i++) { | |
a = (a) ? a : new Sprite(); | |
} | |
result = getTimer()-t; | |
timeText.appendText("func6: "+result+"\n"); | |
t = getTimer(); | |
for (i = 0; i < count; i++) { | |
a = (!a) ? new Sprite() : a; | |
} | |
result = getTimer()-t; | |
timeText.appendText("func7: "+result+"\n"); | |
} | |
private function func1():void { | |
var a:Sprite; | |
a ||= new Sprite(); | |
} | |
// private function func1():void | |
// { | |
// @3 getlocal 0 | |
// @4 pushscope | |
// @5 pushnull | |
// @6 coerce Sprite | |
// @7 setlocal 1 | |
// @10 getlocal 1 (a) | |
// @11 coerce Sprite | |
// @12 dup | |
// @13 convert_b | |
// @14 iftrue @19 | |
// @15 pop | |
// @16 findpropstrict Sprite | |
// @17 constructprop Sprite, 0 args | |
// @18 coerce Sprite | |
// @19 coerce Sprite | |
// @20 setlocal 1 (a) | |
// @22 returnvoid | |
// } | |
private function func2():void { | |
var a:Sprite; | |
if (!a) a = new Sprite(); | |
} | |
// private function func2():void | |
// { | |
// @3 getlocal 0 | |
// @4 pushscope | |
// @5 pushnull | |
// @6 coerce Sprite | |
// @7 setlocal 1 | |
// @10 getlocal 1 (a) | |
// @11 not | |
// @12 iffalse @17 | |
// @13 findpropstrict Sprite | |
// @14 constructprop Sprite, 0 args | |
// @15 coerce Sprite | |
// @16 setlocal 1 (a) | |
// @18 returnvoid | |
// } | |
private function func3():void { | |
var a:Sprite; | |
a = a || new Sprite(); | |
} | |
// private function func3():void | |
// { | |
// @3 getlocal 0 | |
// @4 pushscope | |
// @5 pushnull | |
// @6 coerce Sprite | |
// @7 setlocal 1 | |
// @10 getlocal 1 (a) | |
// @11 coerce Sprite | |
// @12 dup | |
// @13 convert_b | |
// @14 iftrue @19 | |
// @15 pop | |
// @16 findpropstrict Sprite | |
// @17 constructprop Sprite, 0 args | |
// @18 coerce Sprite | |
// @19 coerce Sprite | |
// @20 setlocal 1 (a) | |
// @22 returnvoid | |
// } | |
private function func4():void { | |
var a:Sprite; | |
if (a == null) a = new Sprite(); | |
} | |
// private function func4():void | |
// { | |
// @3 getlocal 0 | |
// @4 pushscope | |
// @5 pushnull | |
// @6 coerce Sprite | |
// @7 setlocal 1 | |
// @10 getlocal 1 (a) | |
// @11 pushnull | |
// @12 ifne @17 | |
// @13 findpropstrict Sprite | |
// @14 constructprop Sprite, 0 args | |
// @15 coerce Sprite | |
// @16 setlocal 1 (a) | |
// @18 returnvoid | |
// } | |
private function func5():void { | |
var a:Sprite; | |
if (a != null) { | |
} else { | |
a = new Sprite(); | |
} | |
} | |
// private function func5():void | |
// { | |
// @3 getlocal 0 | |
// @4 pushscope | |
// @5 pushnull | |
// @6 coerce Sprite | |
// @7 setlocal 1 | |
// @10 getlocal 1 (a) | |
// @11 pushnull | |
// @12 ifne @18 | |
// @14 findpropstrict Sprite | |
// @15 constructprop Sprite, 0 args | |
// @16 coerce Sprite | |
// @17 setlocal 1 (a) | |
// @19 returnvoid | |
// } | |
private function func6():void { | |
var a:Sprite; | |
a = (a) ? a : new Sprite(); | |
} | |
// private function func6():void | |
// { | |
// @3 getlocal 0 | |
// @4 pushscope | |
// @5 pushnull | |
// @6 coerce Sprite | |
// @7 setlocal 1 | |
// @10 getlocal 1 (a) | |
// @11 convert_b | |
// @12 iffalse @16 | |
// @13 getlocal 1 (a) | |
// @14 coerce Sprite | |
// @15 jump @19 | |
// @16 findpropstrict Sprite | |
// @17 constructprop Sprite, 0 args | |
// @18 coerce Sprite | |
// @19 coerce Sprite | |
// @20 setlocal 1 (a) | |
// @22 returnvoid | |
// } | |
private function func7():void { | |
var a:Sprite; | |
a = (!a) ? new Sprite() : a; | |
} | |
// private function func7():void | |
// { | |
// @3 getlocal 0 | |
// @4 pushscope | |
// @5 pushnull | |
// @6 coerce Sprite | |
// @7 setlocal 1 | |
// @10 getlocal 1 (a) | |
// @11 not | |
// @12 iffalse @17 | |
// @13 findpropstrict Sprite | |
// @14 constructprop Sprite, 0 args | |
// @15 coerce Sprite | |
// @16 jump @19 | |
// @17 getlocal 1 (a) | |
// @18 coerce Sprite | |
// @19 coerce Sprite | |
// @20 setlocal 1 (a) | |
// @22 returnvoid | |
// } | |
} | |
} |
Что касается скорости, то от запуска к запуску распределение мест по скорости слишком часто меняется. Нельзя делать каких либо выводов.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ASC 2.0 Лучше оптимизирует именно такие конструкции
private function func1():void {
var a:Sprite;
a ||= new Sprite();
}
Если верить ffdec то останется от такой функции только
getlocal_0
pushscope
pushnull
setlocal_1
getlocal_1
iftrue ofs000f
findpropstrict Qname(PackageNamespace("flash.display"),"Sprite")
constructprop Qname(PackageNamespace("flash.display"),"Sprite") 0
setlocal_1
ofs000f:returnvoid
вместо
getlocal_0
pushscope
pushnull
coerce Qname(PackageNamespace("flash.display"),"Sprite")
setlocal_1
getlocal_1
coerce Qname(PackageNamespace("flash.display"),"Sprite")
dup
iftrue ofs0016
pop
findpropstrict Qname(PackageNamespace("flash.display"),"Sprite")
constructprop Qname(PackageNamespace("flash.display"),"Sprite") 0
coerce Qname(PackageNamespace("flash.display"),"Sprite")
ofs0016:coerce Qname(PackageNamespace("flash.display"),"Sprite")
setlocal_1
returnvoid