Skip to content

Instantly share code, notes, and snippets.

@gok11
Created October 1, 2016 15:30
Show Gist options
  • Select an option

  • Save gok11/1962f80561003e867800bb565d84fd81 to your computer and use it in GitHub Desktop.

Select an option

Save gok11/1962f80561003e867800bb565d84fd81 to your computer and use it in GitHub Desktop.
配列に循環的にアクセスする方法3つの速度を調べてみた ref: http://qiita.com/Gok/items/df12ae2b1b1f5ea13fcb
using UnityEngine;
public class LengthSpeedTest : MonoBehaviour {
int testCount = 1000000;
const int ArrayLength = 1 << 2;
readonly int mask = ArrayLength - 1;
int idx = 0;
int[] intArray = new int[ArrayLength];
// Use this for initialization
void Start () {
idx = 0;
Profiler.BeginSample("ClampPattern1");
clamp1();
Profiler.EndSample();
idx = 0;
Profiler.BeginSample("ClampPattern2");
clamp2();
Profiler.EndSample();
idx = 0;
Profiler.BeginSample("ClampPattern3");
clamp3();
Profiler.EndSample();
UnityEditor.EditorApplication.isPlaying = false;
}
void clamp1()
{
for (int i = 0; i < testCount; i++)
{
if (++idx >= intArray.Length) idx = 0;
intArray[idx] = 0;
}
}
void clamp2()
{
for (int i = 0; i < testCount; i++)
{
intArray[++idx % ArrayLength] = 0;
}
}
void clamp3()
{
for (int i = 0; i < testCount; i++)
{
intArray[++idx & mask] = 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment