forked from: Math.abs
forked from Math.abs (diff: 15)
参考 Mathクラスでよくやる高速化備忘録 http://nulldesign.jp/blog/flash/480.html
ActionScript3 source code
/**
* Copyright shohei909 ( http://wonderfl.net/user/shohei909 )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/cLCA
*/
// forked from zahir's Math.abs
// forked from zahir's Math.max
// forked from zahir's 速度比較用テンプレ
/*
参考
Mathクラスでよくやる高速化備忘録
http://nulldesign.jp/blog/flash/480.html
*/
package {
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.display.Sprite;
import flash.utils.getTimer;
public class FlashTest extends Sprite {
private const LOOP:int = 2000000;
private var t:TextField;
public function FlashTest() {
addChild( ( t= new TextField() ) ).width = t.height = 465;
addEventListener( MouseEvent.CLICK, onClick );
ex();
}
private function onClick( e:MouseEvent ):void
{
ex();
}
private function ex():void{
var i:int = 0;
var time1:int;
var time2:int;
var recode:Array = [];
var count:int = 0;
var abs:Function = Math.abs;
t.text = "各 " + LOOP + " 回ループ\n\n";
// ここから処理
var n:Number;
var a:Number = 1;
var b:Number = Math.PI;
time1 = getTimer();
for( i = 0; i<LOOP; i++ )
{
n = Math.abs( b );
}
time2 = getTimer() - time1;
recode[count++] = new TimeData( "Math Class", time2, "n = " + n );
time1 = getTimer();
for( i = 0; i<LOOP; i++ )
{
n = ( b ^ ( b >> 31 ) ) - ( b >> 31 );
}
time2 = getTimer() - time1;
recode[count++] = new TimeData( "manual", time2, "n = " + n );
time1 = getTimer();
for( i = 0; i<LOOP; i++ )
{
n = abs( b );
}
time2 = getTimer() - time1;
recode[count++] = new TimeData( "Math Class -> local", time2, "n = " + n );
// ここまで処理
/* templete
time1 = getTimer();
for( i = 0; i<LOOP; i++ )
{
}
time2 = getTimer() - time1;
recode[count++] = new TimeData( "", time2, "" );
*/
if( recode.length )
{
$( recode );
}
t.appendText("clickで再計算");
}
private function $( recode:Array ):void
{
var str:String = "";
recode.sortOn( "time", Array.NUMERIC );
var max:int = (recode[ recode.length - 1 ] as TimeData).time;
for( var i:int = 0, len:int = recode.length; i<len; i++)
{
var data:TimeData = recode[i] as TimeData;
var n:int = max / data.time * 100;
str += data.title + " :: " + data.time + " ms\n\t\t最低速に比べて約 " + n + " %高速 \n";
if(data.ms == "") str += "\n";
else str += "\t\t" + data.ms + "\n\n";
}
t.appendText( str );
}
}
}
import flash.geom.Matrix3D;
class TimeData
{
public var title:String;
public var time:int;
public var ms:String; // なんか、おまけ要素
public function TimeData( title:String = "", time:int = 0 , ms:String = "")
{
this.title = title;
this.time = time;
this.ms = ms;
}
}