Comparing if statement with ?: operator on addition
forked from Comparing if statement with ?: operator (diff: 30)
If the value selected by the conditional operator ?: is to be added with the addition assignment operator +=, its operation will come to be slow. Using the assignment operator = instead makes it a little faster. But in this case, the if statement is better to use. JActionScripters "When to use the ?: conditional operator" http://blog.jactionscripters.com/2011/02/09/when-to-use-the-conditional-operator/ 比較している処理の内容については、F-site「条件演算子?:はいつ使うとよいか」をご参照ください。 http://f-site.org/articles/2011/02/09113238.html
ActionScript3 source code
/**
* Copyright Fumio ( http://wonderfl.net/user/Fumio )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/yXKm
*/
// forked from Fumio's Comparing if statement with ?: operator
package {
import flash.display.Sprite;
import flash.utils.getTimer;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
[SWF(width = "240",height = "180")]
public class Test_if_vs_conditional_operator extends Sprite {
private const MAX_NUMBER:uint = 30000000;
private var testData:Vector.<int> = new Vector.<int>(MAX_NUMBER);
private var my_txt:TextField = new TextField();
private var label_txt:TextField = new TextField();
private var my_fmt:TextFormat = new TextFormat();
public function Test_if_vs_conditional_operator() {
// Creating a TextField for display
createTextField();
setData();
// Starting Test
test_conditional_operator();
test_conditional_operator2();
test_conditional_operator3();
test_if();
}
private function test_if():void {
var temp:int = 0;
var started:int = getTimer();
for (var i:int = 0; i < MAX_NUMBER; i++) {
var n:int = testData[i];
if ((n > 0)) {
temp += 1;
} else {
temp += -1;
}
}
xTrace("if (+=)", getTimer() - started);
}
private function test_conditional_operator():void {
var temp:int = 0;
var started:int = getTimer();
for (var i:int = 0; i < MAX_NUMBER; i++) {
var n:int = testData[i];
temp += (n > 0) ? 1 : -1;
}
xTrace("?: operator (+=)", getTimer() - started);
}
private function test_conditional_operator2():void {
var temp:int = 0;
var started:int = getTimer();
for (var i:int = 0; i < MAX_NUMBER; i++) {
var n:int = testData[i];
temp = (n > 0) ? temp + 1 : temp - 1;
}
xTrace("?: operator (=)", getTimer() - started);
}
private function test_conditional_operator3():void {
var temp:int = 0;
var temp2:int;
var started:int = getTimer();
for (var i:int = 0; i < MAX_NUMBER; i++) {
var n:int = testData[i];
temp2 = (n > 0) ? 1 : -1;
temp += temp2;
}
xTrace("?: operator (= to var)", getTimer() - started);
}
private function setData():void {
for (var i:uint = 0; i < MAX_NUMBER; i++) {
testData[i] = Math.pow(-1,i);
}
}
private function createTextField():void {
addChild(my_txt);
addChild(label_txt);
my_fmt.align = TextFormatAlign.RIGHT;
my_txt.x += 50;
my_txt.defaultTextFormat = my_fmt;
my_txt.autoSize = TextFieldAutoSize.RIGHT;
label_txt.autoSize = TextFieldAutoSize.LEFT;
}
private function xTrace(_str:String,n:int):void {
my_txt.appendText(String(n) + "\n");
label_txt.appendText(_str + ":" + "\n");
}
}
}