[Android] Math.Round(丸め)高速版FastMath.round

| トラックバック(0) |

com.android.internal.util (非公開)パッケージに FastMath というクラスがあり、
小数点丸め計算の高速版メソッドがありましたので、どれくらい高速なのか実験してみました。

 

実験用ソースコード 

package com.adakoda.android.fastmathtest;
import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
public class FastMathTestActivity extends Activity {
    // This is copy of com.android.internal.util.FastMath.round method
    public static int round(float x) {
        long lx = (long)(x * (65536 * 256f));
        return (int)((lx + 0x800000) >> 24);
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        long start = SystemClock.uptimeMillis();
        {
            float a = 0.1f;
            for (int i = 0; i < 1000000; i++) {
//                Math.round(a);    // 1.java.lang.Math
                round(a);        // 2.com.android.internal.util.FastMath
            }
        }
        long end = SystemClock.uptimeMillis();
        Log.v("time[ms]", String.valueOf(end - start));
    }
}

 

実験結果

単精度小数点を100万回丸め計算するのに要した時間は、
以下のとおり FastMath.round() の方が、java.lang.Math.round() より 約2.25倍高速でした(≠ 赤い彗星)。

  1. java.lang.Math.round() ・・・ 6,626 [ms]
  2. FastMath.round() ・・・ 2,940[ms]

 

なんだか懐かしい感じです。

関連記事

トラックバック(0)

トラックバックURL: http://mt.adakoda.com/mt-tb.cgi/266

2016年8月

  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31