4
3

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.

Vector3の初期化を計測してみた。

Last updated at Posted at 2018-03-03

Vector3.zero は new Vector3(0,0,0) をしているので遅いと聞いて気になったので計測してみました。
(Unity2017.3)

コード 100万回実施(ms)
var t = new Vector3(1000, 2000, 3000); 19ms
var t = Vector3.zero; 17ms
var t = Vector3.one; 17ms
var t = v; //計測前にキャッシュしてVector3 7ms
var t = v; t.x = 1000; t.y = 2000; t.z = 3000; 12ms

Vetor3.zero は new Vector3() よりは早いですが、キャッシュを代入するのに比べると遅かったです。
キャッシュも、その後に各値代入するとやっぱり其れなりに時間が掛かりました。

benchmark.cs
		int loop = 1000000; // 100万回

		Debug.Log("--------- new Vector3() ------------");

		sw = new System.Diagnostics.Stopwatch();
		sw.Start();
		for (int i = 0; i < loop; i++)
		{
			var t = new Vector3(1000, 2000, 3000);
		}
		Debug.Log(sw.ElapsedMilliseconds + "ms");

		Debug.Log("--------- Vector3.zero ------------");

		sw = new System.Diagnostics.Stopwatch();
		sw.Start();
		for (int i = 0; i < loop; i++)
		{
			var t = Vector3.zero;
		}
		Debug.Log(sw.ElapsedMilliseconds + "ms");

		Debug.Log("--------- Vector3.one------------");

		sw = new System.Diagnostics.Stopwatch();
		sw.Start();
		for (int i = 0; i < loop; i++)
		{
			var t = Vector3.one;
		}
		Debug.Log(sw.ElapsedMilliseconds + "ms");

		Debug.Log("--------- キャッシュを代入 ------------");

		sw = new System.Diagnostics.Stopwatch();
		sw.Start();
		for (int i = 0; i < loop; i++)
		{
			var t = v;
		}
		Debug.Log(sw.ElapsedMilliseconds + "ms");

		Debug.Log("--------- キャッシュを代入してから値を設定 ------------");

		//Vector3 v = Vector3.zero;
		sw = new System.Diagnostics.Stopwatch();
		sw.Start();
		for (int i = 0; i < loop; i++)
		{
			var t = v;
			t.x = 1000;
			t.y = 2000;
			t.z = 3000;
		}
		Debug.Log(sw.ElapsedMilliseconds + "ms");

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?