Permalink
Browse files

added test for conditionals, minor changes in makefile

  • Loading branch information...
1 parent 5abc822 commit 8a550eccf22449ef2cbbd8a113736cde1041ed40 @mrinalwadhwa committed Jun 6, 2011
Showing with 146 additions and 7 deletions.
  1. +2 −2 Makefile
  2. +5 −5 tests/test_bytecodes_iffalse_vs_ifne_vs_ifstrictne.as
  3. +139 −0 tests/test_conditinals.as
View
4 Makefile
@@ -1,10 +1,10 @@
SRCs=$(wildcard tests/*.as)
SWFs=$(SRCs:.as=.swf)
-compile: flexsdk clean $(SWFs)
+compile: flexsdk $(SWFs)
%.swf: %.as
- @ echo "- $@" && \
+ @ echo "- $<" && \
$(MXMLC) -output=$@ \
-library-path+=$(SDK)/frameworks \
-define=CONFIG::Debug,false -define+=CONFIG::Release,true -debug=false \
View
10 tests/test_bytecodes_iffalse_vs_ifne_vs_ifstrictne.as
@@ -1,7 +1,7 @@
/*
-generally observed results:
- Function, Time
+ Observed results:
+ Description, Time
boolean, 61
boolean == , 61
boolean === , 60
@@ -18,8 +18,8 @@ generally observed results:
object == , 312
object === , 237
-seems its is better to use if(variable) except when the variable is a Number and testing for Number == 0
-iffalse uses one less instruction and seems to run at almost the same speed
+ seems its is better to use if(variable) except when the variable is a Number and testing for Number == 0
+ iffalse uses one less instruction and seems to run at almost the same speed
*/
@@ -32,7 +32,7 @@ package
{
override public function test():void
{
- log("Function, Time");
+ log("Description, Time");
var beforeTime:int;
var afterTime:int;
View
139 tests/test_conditinals.as
@@ -0,0 +1,139 @@
+/*
+
+ Test perfomance for various conditionals
+
+
+ Observed results:
+ Description, Time
+ if-else with implicit test for == 0, 203
+ ternary with implicit test for == 0, 205
+ if-else with explicit test for == 0, 202
+ ternary with explicit test for == 0, 201
+ if-else with many conditions, 223
+ ternary with many conditions, 212
+
+ * if-else produces the least bytecode and performs just as well as ternary operator
+ * switch is much slower than if-else or ternary ?: conditional
+ * ternary ?: conditional seems to produce one unnecessary extra instruction in bytecode but execution speed
+ still seems to be about the same
+
+
+ Also see:
+ http://jacksondunstan.com/articles/793
+ http://jacksondunstan.com/articles/1007
+
+*/
+
+package
+{
+ import flash.utils.getTimer;
+ import utils.PerfTest;
+
+ public class test_conditinals extends PerfTest
+ {
+
+ override public function test():void
+ {
+ log("Description, Time");
+
+ var beforeTime:int;
+ var afterTime:int;
+ var i:int;
+ const REPS:int = 10000000;
+ var val:int = 100;
+
+
+
+ // below two tests produce exctly the same bytecode, execution speed is exactly same
+
+ beforeTime = getTimer();
+ for(i = 0; i < REPS; ++i)
+ {
+ if(val) noop();
+ else noop();
+ }
+ afterTime = getTimer();
+ log("if-else with implicit test for == 0, " + (afterTime - beforeTime));
+
+ beforeTime = getTimer();
+ for(i = 0; i < REPS; ++i)
+ {
+ val ? noop() : noop();
+ }
+ afterTime = getTimer();
+ log("ternary with implicit test for == 0, " + (afterTime - beforeTime));
+
+
+
+ // below two tests *do not* produce the same bytecode, ternary uses an extra instruction, but execution speed
+ // still seems to be about the same
+
+ beforeTime = getTimer();
+ for(i = 0; i < REPS; ++i)
+ {
+ if(val == 0) noop();
+ else noop();
+ }
+ afterTime = getTimer();
+ log("if-else with explicit test for == 0, " + (afterTime - beforeTime));
+
+ beforeTime = getTimer();
+ for(i = 0; i < REPS; ++i)
+ {
+ val == 0 ? noop() : noop();
+ }
+ afterTime = getTimer();
+ log("ternary with explicit test for == 0, " + (afterTime - beforeTime));
+
+
+
+
+
+ beforeTime = getTimer();
+ for(i = 0; i < REPS; ++i)
+ {
+ if(val == 0) noop();
+ else if(val == 1) noop();
+ else if(val == 2) noop();
+ else if(val == 3) noop();
+ else noop();
+ }
+ afterTime = getTimer();
+ log("if-else with many conditions, " + (afterTime - beforeTime));
+
+ beforeTime = getTimer();
+ for(i = 0; i < REPS; ++i)
+ {
+ val == 0 ? noop() :
+ val == 1 ? noop() :
+ val == 2 ? noop() :
+ val == 3 ? noop() :
+ noop();
+ }
+ afterTime = getTimer();
+ log("ternary with many conditions, " + (afterTime - beforeTime));
+
+ beforeTime = getTimer();
+ for(i = 0; i < REPS; ++i)
+ {
+ switch (val)
+ {
+ case 0: noop(); break;
+ case 1: noop(); break;
+ case 2: noop(); break;
+ case 3: noop(); break;
+ default: noop();
+ }
+ }
+ afterTime = getTimer();
+ log("switch with many conditions, " + (afterTime - beforeTime));
+
+
+ }
+
+
+
+ private function noop():void {} // a function that does nothing
+
+ }
+}

0 comments on commit 8a550ec

Please sign in to comment.