LoginSignup
3
4

More than 5 years have passed since last update.

【Unity】Transformの回転設定速度比較

Last updated at Posted at 2016-07-10

Transform の回転設定の方法は色々あるがどれが最速なのか検証

【検証環境】

・Unity5.3.5f1(Personal Edition)
・Nexus7(Android 5.0.2)
・10万回試行した際にかかった時間を比較

検証項目

①QuaternionをEulerから計算して設定
②QuaternionをAngleAxisから計算して設定(回転軸キャッシュ版)
③QuaternionをAngleAxisから計算して設定(回転軸簡易取得版)
④Transformに直接Euler角を設定

検証コード

    float prevTime, nowTime;
    int count = 100000;                 // 10万回試行
    Transform trans = this.transform;   // Transform参照処理が含まれないようにする
    Vector3 axis = Vector3.up;          // ※今回回転軸の計算処理は含めない

    prevTime = Time.realtimeSinceStartup;
    for (int i = 0; i < count; ++i)
        trans.localRotation = Quaternion.Euler(0f, 0f, 0f);
    nowTime = Time.realtimeSinceStartup;
    Debug.LogWarning("Quaternion Euler : " + (nowTime - prevTime));

    prevTime = Time.realtimeSinceStartup;
    for (int i = 0; i < count; ++i)
        trans.localRotation = Quaternion.AngleAxis(0f, axis);
    nowTime = Time.realtimeSinceStartup;
    Debug.LogWarning("Quaternion AngleAxis(Cache) : " + (nowTime - prevTime));

    prevTime = Time.realtimeSinceStartup;
    for (int i = 0; i < count; ++i)
        trans.localRotation = Quaternion.AngleAxis(0f, Vector3.up);
    nowTime = Time.realtimeSinceStartup;
    Debug.LogWarning("Quaternion AngleAxis : " + (nowTime - prevTime));

    prevTime = Time.realtimeSinceStartup;
    for (int i = 0; i < count; ++i)
        trans.localEulerAngles = new Vector3(0f, 0f, 0f);
    nowTime = Time.realtimeSinceStartup;
    Debug.LogWarning("Transform Euler : " + (nowTime - prevTime));

結果

①Quaternion Euler
   0.05273438
②Quaternion AngleAxis(Cache)
   0.03561401
③Quaternion AngleAxis
   0.03945923
③Transform Euler
   0.03509521

総評

Euler指定の場合はQuaternionをはさまずに直接指定した方が速い
またTransformに関わらずQuaternionを求める場合はEulerよりAngleAxisで求めた方が速かった

3
4
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
3
4