0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Math.absとMath.floorの代替

Posted at

絶対値を返すMath.abs。
Math.absを利用するより、if文を利用して絶対値を求める方が高速に動作します。

Math.abs 200ms

sample1.as
var n:int = -1;
			
for (var i:uint = 0; i < 1000000; i++) {
  n = Math.abs(n);
}

If文 75ms

sample2.as
var n:int = -1;
			
for (var i:uint = 0; i < 1000000; i++) {
  if (n < 0) {
    n *= -1;
  }
}

指定された数値をもっとも近い整数にするMath.floor。
Math.floorよりも高速に動作する方法を紹介します。
Math.floorと代替方法の実行時間を求めます。

Math.floor 195ms

sample3.as
var pi:Number = 3.141952;

for (var i:uint = 0; i < 1000000; i++) {
  var num1:Number = Math.floor(pi);
}

代替方法 75ms

sample4.as
var pi:Number = 3.141952;

for (var i:uint = 0; i < 1000000; i++) {
  var num1:Number = pi|0;
}

代替方法を利用したほうが高速に動作します。


この記事は、以前ブログで公開していた記事、内容を再編集したものです。
ActionScript 1.0/2.0/3.0とFlex 3の内容が中心です。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?