小数→整数変換の処理時間テスト
小数から整数に変換する際の処理時間のテスト。
基本式)Math.random()*10をCOUNT回足して整数で返す。
(今回はCOUNT=5000000)
Test1:int型の変数に足していく
Test2:事前にintに型変換(int(Math.random()*10))してint型の変数に足していく
Test3:事前に切り捨て処理(Math.floor(Math.random()*10)してint型の変数に足していく
Test4:事前に四捨五入処理(Math.round(Math.random()*10)してint型の変数に足していく
♥0 |
Line 62 |
Modified 2010-11-02 11:44:28 |
MIT License
archived:2017-03-20 02:40:09
ActionScript3 source code
/**
* Copyright geko ( http://wonderfl.net/user/geko )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/gy0i
*/
package {
import flash.display.Sprite;
import flash.text.TextField;
import flash.events.MouseEvent;
public class FlashTest extends Sprite{
static public const COUNT:int = 5000000;
public var txt:TextField;
public function FlashTest() {
txt = addChild(new TextField()) as TextField;
txt.width = stage.stageWidth;
txt.height = stage.stageHeight;
txt.mouseEnabled = false;
getTestTime();
stage.addEventListener(MouseEvent.CLICK, function click(event:MouseEvent):void{
getTestTime();
});
}
public function getTestTime():void{
txt.text = "画面をクリックすると再計測します\n";
for(var i:int = 1; i <= 4; i++){
txt.appendText("\nTest "+String(i)+": "+TestTimer.getTime(this["test"+i])+"ms");
}
}
//***** Test *****//
//normal
public function test1():int{
var sum:int = 0;
for(var i:int = 0; i < COUNT; i++){
sum += Math.random()*10;
}
return sum;
}
//事前にintに型変換
public function test2():int{
var sum:int = 0;
for(var i:int = 0; i < COUNT; i++){
sum += int(Math.random()*10);
}
return sum;
}
//切捨て
public function test3():int{
var sum:int = 0;
for(var i:int = 0; i < COUNT; i++){
sum += Math.floor(Math.random()*10);
}
return sum;
}
//四捨五入
public function test4():int{
var sum:int = 0;
for(var i:int = 0; i < COUNT; i++){
sum += Math.round(Math.random()*10);
}
return sum;
}
}
}
import flash.utils.getTimer;
class TestTimer{
//static public var time:Number;
static public function getTime(func:Function):String{
var startTime:int = getTimer();
return String(func())+" "+String(getTimer()-startTime);
}
}