16
13

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.

this.transformを計測してみた

Last updated at Posted at 2014-07-23

#this.transformがGetComponentを行なっており重いと聞いたので、実際に計測してみた

テストした際のスクリプト

// キャッシュしたtransformを取得するプロパティ
protected Transform mTrans;
public Transform cachedTransform { get { if (mTrans == null)		mTrans = transform; return mTrans; } }
public Transform cachedTransform2 { get { return mTrans; } }

// 01	普通にtransformを取得
for (int i = 0; i < number_of_tests; i++)
{
	Transform test = this.transform;
	//yield return 0;
}

// 02	キャッシュしておくプロパティから取得
for (int i = 0; i < number_of_tests; i++)
{
	Transform test = this.cachedTransform;
	//yield return 0;
}

// 03	ローカル変数にキャッシュしてそれを参照する
Transform cache = this.transform;
for (int i = 0; i < number_of_tests; i++)
{
	Transform test = cache;
	//yield return 0;
}

// 04	cachedTransformプロパティ内の処理を抜き出した版(プロパティのオーバーヘッドをみてみた)
mTrans = null;
for (int i = 0; i < number_of_tests; i++)
{
	if (mTrans == null)
		mTrans = transform;
	Transform test = mTrans;
	//yield return 0;
}

// 05	cachedTransformのifで存在チェックをしない版(プロパティ内の処理負荷の違いをみてみた)
mTrans = this.transform;
for (int i = 0; i < number_of_tests; i++)
{
	Transform test = cachedTransform2;
	//yield return 0;
}

##計測結果

number_of_tests = 100

00: 0.01573563(ms)
01: 0.08511543(ms)
02: 0.005483627(ms)
03: 0.010252(ms)
04: 0.03743172(ms)

number_of_tests = 1000

00: 0.1368523(ms)
01: 0.223875(ms)
02: 0.1990795(ms)
03: 0.1330376(ms)
04: 0.1192093(ms)

number_of_tests = 100(yield return あり)

00: 0.1575947(ms)
01: 0.3004074(ms)
02: 0.01096725(ms)
03: 0.09155273(ms)
04: 0.1583099(ms)

number_of_tests = 1000(yield return あり)

00: 1.351833(ms)
01: 0.9250641(ms)
02: 0.05340576(ms)
03: 0.6370544(ms)
04: 0.4959106(ms)

##計測してみて

・メモリキャッシュにのっている状態であれば、this.transformでもそこそこ早いがローカル変数にキャッシュした方が無難

・ローカル変数でも、キャッシュから外れると重い

・プロパティでもオーバーヘッドが重いようなので、変数にキャッシュしておいた方が軽い

cachedTransformとローカル変数にキャッシュしたものを使うのを場合によって併用するのが作りやすそうか

16
13
1

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
16
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?