FORKED
  1. package {
  2. import flash.display.*;
  3. import flash.geom.*;
  4. import flash.events.*;
  5. import flash.text.*;
  6. import flash.utils.*;
  7. public class Main extends Sprite
  8. {
  9.     static private const _NUM_TIMES:int = 1000000;
  10.     static private const _DEGREES:Number = 30;
  11.     
  12.     static private const _TABLE_SIZE:int = 0x10000;
  13.     static private const _PI:Number = Math.PI;
  14.     static private const _RADIANS:Number = _DEGREES * (_PI / 180);
  15.     static private const _TWO_PI:Number = 2 * _PI;
  16.     static private const _TWO_PI_SCALE:Number = _TABLE_SIZE / _TWO_PI;
  17.     static private const _HALF_PI:Number = _PI / 2;
  18.     
  19.     private var _table:Vector.<Number> = new function ():Vector.<Number>
  20.     {
  21.         var table:Vector.<Number> = new Vector.<Number>(_TABLE_SIZE, true);
  22.         
  23.         for (var i:uint = 0; i < _TABLE_SIZE; i++) {
  24.             table[i] = Math.sin(i / _TWO_PI_SCALE);
  25.         }
  26.         
  27.         return table;
  28.     };
  29.     
  30.     private function _init():void
  31.     {
  32.         _debug(
  33.             "各テスト " + _NUM_TIMES + " 回処理させた計算結果 [単位 : ミリ秒]\n" +
  34.             "(誤差は多少生じます)\n"
  35.         );
  36.         
  37.         _debug("最初に計算の正当性を確認 (テーブルは " + _TABLE_SIZE + " 分割)\n");
  38.         _debug(_DEGREES + " 度の sin を Math.sin() で計算 -> " + Math.sin(_RADIANS));
  39.         _debug(_DEGREES + " 度の sin をテーブルから近似値を取得 -> " + _sin(_RADIANS));
  40.         _debug(_DEGREES + " 度の cos を Math.cos() で計算 -> " + Math.cos(_RADIANS));
  41.         _debug(_DEGREES + " 度の cos をテーブルから近似値を取得 -> " + _cos(_RADIANS) + "\n");
  42.         
  43.         _measure("ループのみ"function ():void
  44.         {
  45.             for (var i:uint = 0; i < _NUM_TIMES; i++) {
  46.                 
  47.             }
  48.         });
  49.         
  50.         _measure("Math.sin() で計算"function ():void
  51.         {
  52.             for (var i:uint = 0; i < _NUM_TIMES; i++) {
  53.                 Math.sin(_RADIANS);
  54.             }
  55.         });
  56.         
  57.         _measure("Math.cos() で計算"function ():void
  58.         {
  59.             for (var i:uint = 0; i < _NUM_TIMES; i++) {
  60.                 Math.cos(_RADIANS);
  61.             }
  62.         });
  63.         
  64.         _measure("関数を通したテーブルから sin の近似値を取得"function ():void
  65.         {
  66.             for (var i:uint = 0; i < _NUM_TIMES; i++) {
  67.                 _sin(_RADIANS);
  68.             }
  69.         });
  70.         
  71.         _measure("関数を通したテーブルから cos の近似値を取得"function ():void
  72.         {
  73.             for (var i:uint = 0; i < _NUM_TIMES; i++) {
  74.                 _cos(_RADIANS);
  75.             }
  76.         });
  77.         
  78.         _measure("テーブルから sin の近似値を直接取得"function ():void
  79.         {
  80.             for (var i:uint = 0; i < _NUM_TIMES; i++) {
  81.                 _table[(_RADIANS * _TWO_PI_SCALE) & (_TABLE_SIZE - 1)];
  82.             }
  83.         });
  84.         
  85.         _measure("テーブルから cos の近似値を直接取得"function ():void
  86.         {
  87.             for (var i:uint = 0; i < _NUM_TIMES; i++) {
  88.                 _table[((_RADIANS + _HALF_PI) * _TWO_PI_SCALE) & (_TABLE_SIZE - 1)];
  89.             }
  90.         });
  91.         
  92.         _debug("\n結果については言及しませんので, 各自ご判断ください.");
  93.     }
  94.     
  95.     private function _sin(radians:Number):Number
  96.     {
  97.         return _table[(radians * _TWO_PI_SCALE) & (_TABLE_SIZE - 1)];
  98.     }
  99.     
  100.     private function _cos(radians:Number):Number
  101.     {
  102.         return _table[((radians + _HALF_PI) * _TWO_PI_SCALE) & (_TABLE_SIZE - 1)];
  103.     }
  104.     
  105.     private var _field:TextField;
  106.     private var _time:uint;
  107.     
  108.     public function Main():void
  109.     {
  110.         _setup();
  111.         _init();
  112.     }
  113.     
  114.     private function _measure(title:String, func:Function, ...params):void
  115.     {
  116.         _time = getTimer();
  117.         func.apply(null, params);
  118.         _time = getTimer() - _time;
  119.         
  120.         _debug("[ " + title + " ] --> " + _time + " ms");
  121.     }
  122.     
  123.     private function _debug(log:String):void
  124.     {
  125.         _field.appendText(log + "\n");
  126.     }
  127.     
  128.     private function _setup():void
  129.     {
  130.         _field = new TextField();
  131.         _field.width = stage.stageWidth - 40;
  132.         _field.height = stage.stageHeight - 60;
  133.         _field.x = 20;
  134.         _field.y = 60;
  135.         _field.multiline = true;
  136.         _field.wordWrap = true;
  137.         
  138.         var format:TextFormat = _field.defaultTextFormat;
  139.         format.font = "_sans";
  140.         _field.defaultTextFormat = format;
  141.         
  142.         addChild(_field);
  143.         
  144.         var button:Sprite = new Sprite();
  145.         button.graphics.lineStyle(1, 0xBBBBBB);
  146.         button.graphics.beginFill(0xEEEEEE);
  147.         button.graphics.drawRoundRect(001002055);
  148.         button.graphics.endFill();
  149.         
  150.         addChild(button);
  151.         
  152.         button.x = 20;
  153.         button.y = 20;
  154.         button.mouseChildren = false;
  155.         button.buttonMode = true;
  156.         
  157.         var field:TextField = new TextField();
  158.         field.width = 100;
  159.         field.height = 20;
  160.         field.htmlText = "<p align='center'><font face='_sans'>再計算</span></p>";
  161.         
  162.         button.addChild(field);
  163.         
  164.         button.addEventListener(MouseEvent.CLICK, function ():void
  165.         {
  166.             _field.text = "";
  167.             _init();
  168.         });
  169.     }
  170. }
  171. }
noswf
Get Adobe Flash Player